【问题标题】:Using javascript to call controller method in MVC在MVC中使用javascript调用控制器方法
【发布时间】:2012-08-27 05:31:21
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc


    【解决方案1】:

    您尝试调用的网址无效:

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

    应该是 "Customer/Details&id=" + id

    (或)

    使用“数据”

    $.ajax({ url: "客户/详细信息", 数据:{id:id}, 成功:函数(){} });

    【讨论】:

      【解决方案2】:

      我会这样做:

      <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() 方法。

      祝你好运!

      【讨论】:

      • 谢谢 :) 只需使用 window.location = "Details/" + id;
      • 太棒了!现在不要忘记使用指针为行设置样式:.MyAction{cursor:pointer;}。如果您需要进一步的帮助,请告诉我。
      【解决方案3】:

      我最近遇到这种情况并选择使用 Ajax 辅助类:

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

      在此示例中,假设您想要一个显示“详细信息”的链接,并且您已经在 Customer 控制器中。

      无论哪种方式,如果您只想从链接触发控制器操作,请查看帮助程序类,在如何传递/处理 id 值等方面为您提供更强大的输入

      【讨论】:

        【解决方案4】:

        有几点:

        1. 在操作和参数之间添加一个斜杠 (/):url: "Customer/Details/" + id,否则,您将调用一个名为Details123Action,例如,它不会不存在;
        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 = ""}
        );
        

        【讨论】:

        • 谢谢! 1. 完成! 2. 完成! 3.什么??怎么做?
        • @ChristianThoressonDahl:你的 Global.asax 是什么样的?
        • @ChristianThoressonDahl:请查看我的更新并添加新路线。
        • 用 global.asax 中的新路线更新了我的帖子。还是行不通。我一定是做错了什么……在电脑前呆了几个小时后,如果这只是一件愚蠢的事情,我不会感到惊讶……:)
        • @ChristianThoressonDahl:你的路线错了,不是{controller}/{action}/{id},是Customer/Details/{id}
        【解决方案5】:

        您的代码中有一个错字

        success:
        //----^
        

        【讨论】:

          猜你喜欢
          • 2016-04-08
          • 2014-07-13
          • 1970-01-01
          • 1970-01-01
          • 2023-02-07
          • 2015-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多