asp.net-mvc – 从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菜单中的“创建新零件”时,我会收到异常。请帮我解决这个问题。

解决方法

您正在使用不正确的重载。你应该使用这个超载
public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,string linkText,string actionName,string controllerName,Object routeValues,Object htmlAttributes
)

正确的代码将是

<%= Html.ActionLink("Create New Part",new { parentPartId = 0 },null)%>

请注意,额外的参数在最后。
对于其他重载,请访问LinkExtensions.ActionLink Method.您可以看到没有您尝试使用的字符串,字符串,字符串,对象重载。

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...