从Html.ActionLink传递参数控制器动作 [英] Passing parameter to controller action from a Html.ActionLink

查看:706
本文介绍了从Html.ActionLink传递参数控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么不对的这个网站?我想在母版的链接导航到CreateParts的看法。我有行动CreateParts,这在该控制器明细表参数parentPartId。

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);
    }

当我在SITEMASTER的菜单中点击创建新零件,我得到异常。请大家帮我出这一点。

When I click on 'Create New Part' in the menu in SiteMaster, I get exception. Please help me out of this.

推荐答案

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

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
) 

和正确的code将

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

请注意在年底额外的参数。
对于其他重载,请访问<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx\">LinkExtensions.ActionLink方法。正如你可以看到有没有你要使用字符串,字符串,字符串对象超载。

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.

这篇关于从Html.ActionLink传递参数控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆