0

我在控制器中有一个动作:

    public ActionResult Close(DocType docType)
    {
        return View();
    }

其中 DocType 是一个简单的枚举。我想有 2 个不同的链接指向相同的操作,但参数不同。我试过这个:

    <mvcSiteMapNode title="Accounting" clickable="false" imageUrl="~/Content/Images/Buttons/MenuButtons/billing.png" visibility="path">
       <mvcSiteMapNode title="Payments" controller="Payment" action="Index"></mvcSiteMapNode>
       <mvcSiteMapNode title="Closing WO" controller="Payment" action="Close" docType="2"></mvcSiteMapNode>
       <mvcSiteMapNode title="Closing WS" controller="Payment" action="Close" docType="4"></mvcSiteMapNode>
   </mvcSiteMapNode>

但在菜单中,我有 2 个没有任何参数的链接:“/Payments/Close”

怎么了?如何在 mvcSiteMapNode 中添加参数?

这是我的RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
4

1 回答 1

0

如果你想使用你的默认路由,你必须使用id路由键(因为它只支持键controlleractionid)。如果你不这样做,你会得到一个带有查询字符串的路由?docType=2,因为这是额外的未定义信息,不是路由的一部分。

<mvcSiteMapNode title="Accounting" clickable="false" imageUrl="~/Content/Images/Buttons/MenuButtons/billing.png" visibility="path">
    <mvcSiteMapNode title="Payments" controller="Payment" action="Index"></mvcSiteMapNode>
    <mvcSiteMapNode title="Closing WO" controller="Payment" action="Close" id="2"></mvcSiteMapNode>
    <mvcSiteMapNode title="Closing WS" controller="Payment" action="Close" id="4"></mvcSiteMapNode>
</mvcSiteMapNode>

public ActionResult Close(DocType id)
{
    return View();
}

否则,您需要有一条带有 key 的路线{docType}。无论哪种方式,键名都必须匹配才能正确生成 URL(因为在 MVC 中使用 时需要这样做ActionLink)。

于 2017-02-16T15:50:20.593 回答