Can I build an ActionLink through a Helper class? How?
I'm using a helper class to build my hyperlink. The markup looks like this:
<a href='/controller/action?id=val'></a>
This is the same markup that the Html.ActionLink()
produces. However, if I use the Html.ActionLink()
, the id is accepted as a parameter in the method inside the controller. If I just generate the a
tag in a string like the above I get the error below when I try to define the method with an id parameter:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for 开发者_JAVA技巧method 'System.Web.Mvc.ActionResult' ... To make a parameter optional its type should be either a reference type or a Nullable type. Parameter name: parameters
- Is there a way to use
Html.ActionLink()
in my helper class? - Why is there a difference between the two techniques?
Here's my controller action:
public ActionResult AssignmentAdd(int id)
{
return View();
}
It looks like you have a routing problem. I guess your controller action has to be modified to accept a nullable integer for the id parameter:
public ActionResult Action(int? id)
{ }
Also if you followed the default routing table as generated by the template the correct link for this action should be:
<a href="/controller/action/val"></a>
And the helper to generate it:
<%= Html.ActionLink("link text", "action", new { id = "val" }) %>
精彩评论