jquery - ASP.NET MVC 4 使用 jquery 打开 Bootstrap 模式

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

我尝试使用 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();
    })
});

在客户端 Controller

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

“编辑”操作有效,但“产品”操作无效(我设置了一个断点,但未调用它)。怎么了?

最佳答案

问题出在参数名称

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

关于jquery - ASP.NET MVC 4 使用 jquery 打开 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421093/

相关文章:

javascript - 使用动态配置选项动态加载 highcharts

jquery - 动态更改宽度和高度属性

c# - ASP.NET MVC : Programmatically set HTTP headers on static content

asp.net-mvc - Web Api 多次获取具有相同签名的路由

javascript - 模态窗口内的 iFrame 在 Chrome 上不起作用

javascript - 无法读取未定义 jQuery 的属性 'top'

javascript - 如何检测 "search"HTML5 输入的清除?

javascript - 展开菜单,显示子菜单

css - Sass::SyntaxError 要导入的文件未找到或不可读:样式

asp.net-mvc - 使用与本地化更改冲突的自定义数据注释进行验证