开发者

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
  1. Is there a way to use Html.ActionLink() in my helper class?
  2. 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" }) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜