将参数从 Html.ActionLink 传递到控制器操作
这个html有什么问题吗?我想在母版页中有一个链接来导航到“CreateParts”视图。我有操作“CreateParts”,它在控制器“PartList”中有一个参数parentPartId。
<li id="taskAdminPartCreate" runat="server">
<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 })%></li>
我的控制器操作就像
public ActionResult CreateParts(int parentPartId)
{
HSPartList objHSPart = new HSPartList();
objHSPart.Id = parentPartId;
return View(objHSPart);
}
当我单击 SiteMaster 菜单中的“创建新零件”时,出现异常。请帮助我摆脱这个困境。
Is there anything wrong with this html? I want to have a link in the masterpage to navigate to "CreateParts" view. I have action 'CreateParts' which have a parameter parentPartId in the controller 'PartList'.
<li id="taskAdminPartCreate" runat="server">
<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 })%></li>
My controller action is like
public ActionResult CreateParts(int parentPartId)
{
HSPartList objHSPart = new HSPartList();
objHSPart.Id = parentPartId;
return View(objHSPart);
}
When I click on 'Create New Part' in the menu in SiteMaster, I get exception. Please help me out of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的过载不正确。您应该使用此重载
并且正确的代码是
注意末尾的额外参数。
对于其他重载,请访问 LinkExtensions.ActionLink方法。正如您所看到的,您没有尝试使用
string, string, string, object
重载。You are using incorrect overload. You should use this overload
And the correct code would be
Note that extra parameter at the end.
For the other overloads, visit LinkExtensions.ActionLink Method. As you can see there is no
string, string, string, object
overload that you are trying to use.您使用的 ActionLink 重载不正确。试试这个
You are using the incorrect overload of ActionLink. Try this
除了已接受的答案之外:
如果您要使用
此选项,将创建操作链接,您无法为链接创建新的自定义属性或样式。
然而,ActionLink 扩展中的第四个参数将解决这个问题。使用第四个参数以您的方式进行定制。
Addition to the accepted answer:
if you are going to use
this will create actionlink where you can't create new custom attribute or style for the link.
However, the 4th parameter in ActionLink extension will solve that problem. Use the 4th parameter for customization in your way.