我尝试使用 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/