加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 从Html.ActionLink将参数传递给控制器??操作

发布时间:2020-12-15 18:34:36 所属栏目:asp.Net 来源:网络整理
导读:这个html有什么问题吗?我想在主页面中有一个链接导航到“CreateParts”视图。我有动作’CreateParts’在控制器’PartList’中有一个参数parentPartId。 li id="taskAdminPartCreate" runat="server" %= Html.ActionLink("Create New Part","CreateParts","P
这个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.您可以看到没有您尝试使用的字符串,字符串,字符串,对象重载。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读