【问题标题】:ASP.NET MVC 4 Open Bootstrap modal with jqueryASP.NET MVC 4 使用 jquery 打开引导模式
【发布时间】:2016-05-24 18:21:01
【问题描述】:

我正在尝试使用 asp.net mvc 4 + jquery 在特定路径中打开模式,代码如下:

卡片.cshtml

<div class="btn-group">
    <button class="btn btn-primary openModal" data-path="/Client/Edit/@Model.ClientID">Editar</button>
    <button class="btn btn-primary openModal" data-path="/Client/Products/@Model.ClientID">Products</button>
</div>

然后在modal.js中

$(document).on('click', '.openModal', function () {
    var path = $(this).attr("data-path");
    $("#modal").load(path, function () {
        $("#modal").modal();
    })
});

在客户端控制器中

public ActionResult Edit(long id = 0)
{
    Client client = db.Clients.Find(id);

    if (client == null)
    {
        return HttpNotFound();
    }

    return View(client);
}

public ActionResult Products(long clientID)
{
    return View(db.Products.Where(p => p.ClientID == clientID).ToList());
}

Edit 操作有效,但 Products 操作无效(我放置了一个断点,但没有调用它)。怎么了?

【问题讨论】:

  • 你检查响应了吗,有没有错误?
  • 浏览器只是像“编辑”模式打开时一样“淡出”,但没有发生,控制器的“产品操作”没有被调用,但是在chrome上检查jquery代码的“路径”属性是正确的

标签: jquery twitter-bootstrap asp.net-mvc-4 bootstrap-modal


【解决方案1】:

问题是参数的名称

public ActionResult Products(long clientID)
{
    return View(db.Products.Where(p => p.ClientID == clientID).ToList());
}

从 clientID 更改为 id 并且可以正常工作。这是因为默认路由是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2017-09-27
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多