Html.ActionLink with a specified HTML id?

前端 未结 4 867
青春惊慌失措
青春惊慌失措 2021-02-02 06:47

I\'d like to give the like generated with an Html.ActionLink an HTML id so I can change the CSS depending on where I am. I have a MasterPage with a set

4条回答
  •  后悔当初
    2021-02-02 07:45

    You were on the right track. I am not sure why it didn't work for you as your code has a typo which would have produced a } expected error. The following is what you are looking for:

     <%= Html.ActionLink("Test Link", "SomeAction", "SomeController",
             null, new {id = "someID" }) %> 
    

    Which produces teh following HTML:

    Test Link
    

    Edit: I just realized what the issue is because I was mis-reading what you tried. You are using the wrong overload to pass in the id html element. Your probably passing the new { id="blah" } param into the routeValues parameter, which will cause it to be used when building the route link, rather than the htmlAttributes paramater which is what you want.

    I think you are using:

    ActionLink(string linkText, string actionName, Object routeValues,
        Object htmlAttributes)
    

    When what you need to use is the following overload like I did above in my answer:

    ActionLink(string linkText, string actionName, string controllerName,
        Object routeValues, Object htmlAttributes)
    

    Which makes sure new { id="blah" } is being passed into the htmlAttributes param.

提交回复
热议问题