4

我试图让一个表格行作为我的 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);
}
4

5 回答 5

5

这是我要做的:

<tr data-id='@SomeRazorDataId' class="MyAction">foo</tr>

接着:

$(document).ready(function(){
    $('.MyAction').live("click",function () {
        var id = $(this).attr('data-id');
        window.location = "Customer/Details/" + id;
    })
});

如果你使用 jQuery 1.7+,你应该使用on()方法而不是live()方法。

祝你好运!

于 2012-09-01T22:28:33.990 回答
3

您的代码中有错字

success:
//----^
于 2012-09-01T21:35:21.253 回答
2

有几件事:

  1. 在操作和参数之间添加一个斜杠( ): ,否则,您将调用一个被调用的,例如,它不存在;/url: "Customer/Details/" + idActionDetails123
  2. 确保您在 Global.asax 中配置了路由以支持id,即Customer/Details/{id}
  3. 就像@undefined 所说,回调的名称是success,而不是succes

Global.asax应该有这些方面的东西:

routes.MapRoute(
  "CustomerDetails",
  "Customer/Details/{id}",
  new { controller = "Customer", action = "Details", id = ""}
);
于 2012-09-01T21:39:53.647 回答
1

I had this type of situation lately and opted to use the Ajax helper class:

        @Ajax.ActionLink("Details", "Details", 
        new
        {
            id = Model.Id
        }, null)

In this example it would assume that you want a link saying 'details' and you're already in the Customer controller.

Either way, look at the helper classes if all you want to do is fire a controller action from a link, gives you a bit more strong typing in terms of how to pass/handle the id value etc

于 2012-09-01T22:01:06.707 回答
0

您尝试调用的 url 无效:

“客户/详细信息”+ id,

相反,它应该是"Customer/Details&id=" + id

(或者)

使用“数据”

$.ajax({ url: "客户/详细信息", data:{id:id}, succes: function () { } });

于 2012-09-20T08:06:40.510 回答