Ajax.ActionLink Only链接到当前页面

时间:2014-02-07 23:02:31

标签: javascript jquery ajax asp.net-mvc-4

我在Partial View中有一个Ajax.ActionLink,如下所示:

<div id="accordion">
    @foreach (var m in Model)
    {
        var targetId = m + "_List";

        <h3 id="@(m)" class="thingModelHeader">
            @Ajax.ActionLink(m, "AjaxGetThings", "Perf",
                new AjaxOptions { 
                    HttpMethod="GET",
                    InsertionMode = InsertionMode.Replace,
                     UpdateTargetId= targetId,
                     Url="13412" 
                })
        </h3>
        <div id="@targetId"  >
            @* list will populate here when the link is clicked *@
        </div>

    }
</div>

我有一个控制器,使用适当的方法:

public class PerfController : Controller
{
    [Route("AjaxGetThings/{id}", Name = "AjaxGetThings")]
    public PartialViewResult AjaxGetThings(string id)
    {
        IQueryable<Thing> results;

        using (var repo = new ReadOnlyRepository<Thing>())
        {
            things = repo.All()
                          .Where(p => p.Id == Id)
                          .OrderBy(p => p.Name)
                          ;
        }

        return PartialView("CustomPartialView", results);
    }

}

我有一个ScriptBundle用于不引人注目的验证:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
    "~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));

...我在布局页面中引用它:

    </footer>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jquery-ui")
    @Scripts.Render("~/bundles/jqueryval")
    @RenderSection("scripts", required: false)
  </body>
</html>

问题在于,无论我如何在AjaxOptions中定义Action,Controller或Url,链接始终指向当前URL。目标div加载并更新整个页面的'nuther版本 - 所以它的工作就其Ajax部分而言,但无论我做什么,URL都不会指向任何有用的地方 - 比如我指定的实际控制器和操作。

1 个答案:

答案 0 :(得分:0)

尝试像这样指定网址

url: 'Url.Action("Action", "Controller", new { ID = m.targetid })'