" + "Html.ActionLink(" + "Home" + ", " + "Dashboard" + ", " + "User" +...">

How to visualize an actionlink to view a form controller

ViewBag.htmml = "<li>" + "Html.ActionLink(" + "Home" + ", " + "Dashboard" + ", " + "User" + ")" + "</li>"; 

Above is my code using which I am trying to make ActioLink for my view. When the code is run using the following code, I don't get an actionlink named Home, instead I get Html.ActionLink(Home, Dashboard, User) in my <li> . How to get ActionLink with what I'm trying.

  @Html.Raw(ViewBag.htmml) 
0
source share
1 answer

As @Stephen Muecke pointed out, HTML should be generated in the view, not in the controller.

However, if you insist on creating it in the controller, you need to have an HtmlHelper to call the ActionLink method. But, as the name implies, HtmlHelper is only available automatically to you in the view.

You can create it yourself (using, for example, the solution from this answer ), but it is very inefficient and error prone.

But since you are only trying to create an <a> element, and you are already creating the elements yourself (for example, the <li> element), you can simply do this (using UrlHelper , which is available in the controller):

 ViewBag.htmml = "<li><a href=\"" + Url.Action("Dashboard", "User") + "\">Home</a></li>"; 
0
source

Source: https://habr.com/ru/post/987125/


All Articles