Maybe a duplicate, but can't figure how to write a method in an MVC View .cs file callable to return the equivalent of @Html.ActionLink in a cshtml file.
Kind of like:
public string BrowseMenu
{
return "<div><p>" + Html.ActionLink(" linktext ", "Action", "Controller") + "</p></div>";
}
What I want to do is return the same HTML structure with some ActionLinks in it to a bunch of different View pages in an MVC Controller, so if I am going about it the entire wrong way, that's where I am trying to get to.
You need to return an ActionResult
from your controller to view, if you want to render some html. Otherwise, view will receive a string and display it as it is.
You can define a partial view which holds the html, and you can render this partial view in your main view.
public PartialViewResult MyActionLink()
{
return PartialView("_MyPartialView");
}
_MyPartialView
is just a view file holding the html you want to render.
However, I would suggest writing a custom html helper which I believe cleaner solution.
public static string MyActionLink(this HtmlHelper helper)
{
StringBuilder sb = new StringBuilder();
sb.Append("<div><p>");
sb.Append(helper.ActionLink("Text","Action","Controller"));
sb.Append("</p></div>")
return sb.ToString();
}
Then you can render this custom html helper in your view via
@Html.MyActionLink()