1

I have a list like below, which is fine:

<ul class="vlistmenu">
    @foreach (var m in Model)
    {
        <li class="forum">@Html.ActionLink(m.LinkName, "Details", "Forum", new { id = m.Id }, null)</li>
    }
</ul>

Now I'm trying to format the resulting MvcHtmlString from the call to Html.ActionLink and add some more information. But the sample below is not working as I was hoping for.

<ul class="vlistmenu">
    @foreach (var m in Model)
    {
        var link = m.LinkName + "<br /><a style=\"font-size:smaller\">By: Ronron (July 11, 2011)</a>");
        <li class="forum">@Html.ActionLink(link, "Details", "Forum", new { id = m.Id }, null)</li>
    }
</ul>

Basically what I'd like is to append the literal string "By: Ronron (Jull 11, 2011)" to the info from my model, which is m.LinkName. However, I'd like that m.LinkName's font be bigger than the appended literal string. In addition, the literal string should start on the next line not immediately next to m.LinkName. The combined m.LinkName and appended string will form one action link.

Ronald
  • 1,532
  • 4
  • 18
  • 34

1 Answers1

2

if i understood correctly you want to write html attribute to a element so u can declare it on html.actionlink html attributes as you can see on the code ( if you want to add class attribute you need write with @symbol like : new { style = "font-size:smaller", @class = "xClass"}

<ul class="vlistmenu">
@foreach (var m in Model)
{
    var link = m.LinkName + "<br /><a style=\"font-size:smaller\">By: Ronron (July 11, 2011)</a>");
    <li class="forum">@Html.ActionLink(link, "Details", "Forum", new { id = m.Id },  new { style = "font-size:smaller"})</li>
}

Addition for your comment, you can write a custom helper

Raw ActionLink linkText

or you can make manual a tag via url.action method so it will give links according to your controller action but tags implemented manually

<a href="@Url.Action("Details","Forum",new { id = m.Id })">@m.linkname<br /><span style="font-size:smaller">By: Ronron (July 11, 2011)</span></a>
Community
  • 1
  • 1
c0demaster
  • 738
  • 6
  • 17
  • @Codemaster, thank you for your reply. English is not my natural language and it looks like it's not working for me in this specific post. What I'd like is to append the literal string "By: Ronron (Jull 11, 2011)" to the info from my model, which is m.LinkName. However, I'd like that m.LinkName's font be bigger than the appended literal string. In addition, the literal string should start on the next line (
    ?) not immediately next to m.LinkName.
    – Ronald Aug 20 '11 at 03:45
  • This answer will do it, however I would suggest putting the line break in by using css instead of using a `
    ` element because it doesn't seem to have the correct semantic meaning here.
    – Nick Larsen Aug 20 '11 at 11:28
  • what he is trying to do is bad practice, it's spaghetti code all over again – mare Aug 20 '11 at 22:04
  • @mare, if you have a good idea I will appreciate it. It doesn't help just mentioning about the negatives and not offering the better solution. Just saying. – Ronald Aug 21 '11 at 10:31
  • better solution would probably use some combination of custom HTML helpers, partial views and `string.Format()`. – mare Aug 22 '11 at 18:41