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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8293934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:36:35  来源:igfitidea点击:

Passing parameter to controller action from a Html.ActionLink

asp.net-mvchtml.actionlink

提问by Suja Shyam

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'.

这个html有什么问题吗?我想在母版页中有一个链接以导航到“CreateParts”视图。我有动作“CreateParts”,它在控制器“PartList”中有一个参数 parentPartId。

<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.

当我单击 SiteMaster 菜单中的“创建新部件”时,出现异常。请帮我解决这个问题。

回答by archil

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, objectoverload that you are trying to use.

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

回答by krolik

You are using the incorrect overload of ActionLink. Try this

您正在使用错误的 ActionLink 重载。尝试这个

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

回答by Md. Tazbir Ur Rahman Bhuiyan

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.

这将创建 actionlink,您无法为链接创建新的自定义属性或样式。

However, the 4th parameter in ActionLink extension will solve that problem. Use the 4th parameter for customization in your way.

然而,在ActionLink的扩展4个参数会解决这个问题。使用第四个参数以您的方式进行自定义。

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