Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Html.ActionLink in a custom HTML helper

I am designing a custom HTML helper and I would like to execute Html.ActionLink to provide dynamic URL generation.

    namespace MagieMVC.Helpers     {         public static class HtmlHelperExtension         {             public static string LinkTable(this HtmlHelper helper, List<Method> items)             {                 string result = String.Empty;                  foreach (Method m in items)                 {                     result += String.Format(                         "<label class=\"label2\">{0}</label>" +                         System.Web.Mvc.Html.ActionLink(...) +                         "<br />",                         m.Category.Name,m.ID, m.Name);                 }                  return result;             }     } } 

Unfortunately Html.ActionLink is not recognized in this context whatever the namespace I have tried to declare.

As a generic question, I would like to know if it is possible to use any existing standard/custom Html helper method when designing a new custom helper.

Thanks.

like image 828
Sylvain Avatar asked Mar 04 '10 13:03

Sylvain


People also ask

How do you add an extension to the HTML helper?

Creating HTML Helpers with Static Methods The easiest way to create a new HTML Helper is to create a static method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label> .

How do I transfer my ActionLink model to my controller?

You'll have to serialize your model as a JSON string, and send that to your controller to turn into an object. Please don't format with snippets when the language used is not displayable by them. It doesn't improve the answer and makes your answer more cluttered. Regular code blocks will work fine.

Which of the following HTML helper will allows to call an action from a controller?

Here in Html. Beginform we are passing View name (Index), Controller name (Home) and declare formMethod = Post Method. If you are not passing any parameter like View name and Controller name then it will automatically call the current controller action method having [HttpPost] attribute.


2 Answers

Don't you have the helper already?

helper.ActionLink("text", "actionName"); 

Don't forget to include using System.Web.Mvc.Html namespace.

And yes, you could use the existing extension methods as long as you included the necessary namespaces.

like image 89
Çağdaş Tekin Avatar answered Sep 28 '22 00:09

Çağdaş Tekin


FYI, for MVC 3, I found the ActionLink here:

System.Web.Mvc.Html.LinkExtensions.ActionLink(text, action, controller).ToHtmlString(); 
like image 20
LA Guy 88 Avatar answered Sep 28 '22 01:09

LA Guy 88