我试图让一个表格行作为我的 mvc 网站中另一个视图的链接。我不想使用自动生成的表格列表提供的标准“详细信息”链接,而是使用表格行作为“详细信息”视图的链接。所以我需要以某种方式使该行作为链接工作。每个 rom 都有一个唯一的 id,我需要将其传递给控制器方法。我尝试了不同的解决方案,但注意到当我按下表格行时会发生......
到目前为止,这就是我所拥有的:
<script type="text/javascript">
$(document).ready(function(){
$('#customers tr').click(function () {
var id = $(this).attr('id');
$.ajax({
url: "Customer/Details" + id,
succes: function () { }
});
})
})
</script>
我的控制器方法:
public ActionResult Details(int id)
{
Customer model = new Customer();
model = this.dbEntities.Customers.Where(c => c.Customer_ID == id).Single();
return View(model);
}
全球.asax:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"CustomerDetails",
"Customer/Details/{id}",
new { controller = "Customer", action = "Details", id = "" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}