How do I add a class to an @Html.ActionLink?

后端 未结 5 455
旧巷少年郎
旧巷少年郎 2020-12-01 09:29

I have two @Html.ActionLink\'s that I want to make look like buttons. I\'m able to make this happen with CSS but only if I use the #ID of the actio

相关标签:
5条回答
  • 2020-12-01 09:52

    The problem is that class is a reserved word in C#. You can specify that you want to use the name 'class' as your attribute name by escaping it with the @ symbol like so:

     @Html.ActionLink("Print PO", "PoReport", new { id = 51970}, new { id = "PoPrint", @class = "PoClass"})
    
    0 讨论(0)
  • 2020-12-01 09:52

    You have to indicate the action name, controller and url parameters (null in this example)

    @Html.ActionLink("Link Name", 
    "ActionName",
    "ControllerName",
    null,
    new { @class = "your css class" }
    )
    
    0 讨论(0)
  • 2020-12-01 09:54

    You have to use the @ character, since class is a keyword in C#. Here's a link to the MSDN documentation: http://msdn.microsoft.com/en-us/library/dd492124(v=vs.108).aspx

    @Html.ActionLink("Link Text", "ActionName", 
             new { controller = "MyController", id = 1 }, 
             new { @class = "my-class" })
    
    0 讨论(0)
  • 2020-12-01 10:07

    Do this new { @class = "PoClass"} You need the @ for keywords like class

    0 讨论(0)
  • 2020-12-01 10:07

    It worked when i use null

    @Html.ActionLink("Add User", "Create",null, new { @class = "btn btn-primary" })

    "Add User" is Button Text

    "Create" is actionName

    null

    new { @class = "btn btn-primary" } CSS used bootstrap class

    0 讨论(0)
提交回复
热议问题