Url.Action方法没有生成正确的URL

时间:2013-12-22 04:22:45

标签: jquery asp.net-mvc-5

我在@Url.Action()方法的jQuery脚本中有两个调用,我无法弄清楚为什么第一个调用生成正确的URL但第二个调用没有。

    .jqGrid( 'navButtonAdd', "#SectionsPager", {
        caption: "Add ",
        title: "Add new section.",
        buttonicon: "ui-icon-add",
        onClickButton: function ()
        {
            location.assign( "@Url.Action("Index", "Home")" );
        }
    } )
    .jqGrid( 'navButtonAdd', "SectionsPager", {
        caption: "Edit ",
        title: "Edit selected cell.",
        buttionicon: "ui-icon-edit",
        onClickButton: function ()
        {
            location.assign( "@Url.Action("EditSection")");
        }
    } );

如果我查看页面的源,则第一次调用会正确生成URL /DataArea/(控制器和视图位于名为“DataArea”的Area中)。但第二个调用只生成/DataArea/Sections/,它是它所呈现的页面的URL。我甚至试过

"@Url.Action("EditSection", "Section")");

但它做同样的事情。为什么第二次调用没有生成正确的URL?

1 个答案:

答案 0 :(得分:0)

我已修好它,但我不确定它为什么会被打破。在我使用的.cs文件中:

    [Route]
    public ActionResult EditSection()
    {
        return View();
    }

根据我能找到的所有文档,这意味着路由应该是/EditSection,如果我在浏览器中键入URL,我甚至可以通过该路由访问它。在玩了一下后我最终使用

    [Route("EditSection")]
    public ActionResult EditSection()
    {
        return View();
    }

现在@Url.Action("EditSection")正常工作。我认为这可能是新mvc5属性路由中的一个错误。如果我能确定它确实是一个错误,我将更多地使用它并向MS发送报告。