【问题标题】:Ajax.ActionLink Only Links to the Current PageAjax.ActionLink 仅链接到当前页面
【发布时间】:2014-02-07 23:02:31
【问题描述】:

我在部分视图中有一个 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 永远不会指向任何有用的地方 - 比如我指定的实际控制器和操作。

【问题讨论】:

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


    【解决方案1】:

    尝试像这样指定网址

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

    【讨论】:

    • 如何将 id 传递到该 url?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2021-05-04
    • 2012-09-05
    相关资源
    最近更新 更多