Asp.net mvc 将参数从Html.ActionLink传递给控制器操作
这个html有什么问题吗?我想在母版页中有一个链接来导航到“CreateParts”视图。我有一个操作“CreateParts”,它在控制器“PartList”中有一个参数parentPartIdAsp.net mvc 将参数从Html.ActionLink传递给控制器操作,asp.net-mvc,html.actionlink,Asp.net Mvc,Html.actionlink,这个html有什么问题吗?我想在母版页中有一个链接来导航到“CreateParts”视图。我有一个操作“CreateParts”,它在控制器“PartList”中有一个参数parentPartId <li id="taskAdminPartCreate" runat="server"> <%= Html.ActionLink("Create New Part", "CreateParts", "PartLi
<li id="taskAdminPartCreate" runat="server">
<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 })%></li>
当我在SiteMaster的菜单中单击“创建新零件”时,我得到异常。请帮我解决这个问题。您使用的ActionLink重载不正确。试试这个
<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 }, null)%>
您使用的重载不正确。你应该使用这个重载
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
正确的代码应该是
<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 }, null)%>
请注意,末尾有一个额外的参数。
有关其他重载,请访问。正如您所看到的,没有您试图使用的
字符串、字符串、字符串、对象
重载。除了接受的答案之外:
如果你打算使用
@Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 },null)
这将创建actionlink,您无法为该链接创建新的自定义属性或样式
然而,ActionLink扩展中的第四个参数将解决这个问题。按照您的方式使用第四个参数进行自定义
@Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 }, new { @class = "btn btn-info", @target = "_blank" })
在这个问题上添加一个例外会很好:)伙计,runat=server在那个可怜的li上到底在做什么?不要强奸MVC:)鲁昂,你能在asp.net MVC中提供最佳实践吗?这不是最佳实践,这是基本概念。了解runat=server在WebFrom中的作用,以及为什么MVC中不需要它。我建议先阅读一些“背景”知识,比如WebForm和MVCYou's great@archill背后的模式。你节省了很多时间,谢谢汉克斯,我错过了null。
@Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 }, new { @class = "btn btn-info", @target = "_blank" })