将参数从 Html.ActionLink 传递到控制器操作

发布于 2024-12-18 03:41:00 字数 606 浏览 2 评论 0原文

这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-12-25 03:41:00

您使用的过载不正确。您应该使用此重载

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)%>

注意末尾的额外参数。
对于其他重载,请访问 LinkExtensions.ActionLink方法。正如您所看到的,您没有尝试使用string, string, string, object 重载。

You are using incorrect overload. You should use this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
) 

And the correct code would be

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

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.

乞讨 2024-12-25 03:41:00

您使用的 ActionLink 重载不正确。试试这个

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

You are using the incorrect overload of ActionLink. Try this

<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 }, null)%>
北渚 2024-12-25 03:41:00

除了已接受的答案之外:

如果您要使用

 @Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 },null)

此选项,将创建操作链接,您无法为链接创建新的自定义属性或样式。

然而,ActionLink 扩展中的第四个参数将解决这个问题。使用第四个参数以您的方式进行定制。

 @Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 }, new { @class = "btn btn-info", @target = "_blank" })

Addition to the accepted answer:

if you are going to use

 @Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 },null)

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.

 @Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 }, new { @class = "btn btn-info", @target = "_blank" })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文