【问题标题】:ajax get to MVC method not passing parametersajax 获取 MVC 方法不传递参数
【发布时间】:2012-03-19 05:56:44
【问题描述】:

我有一个看起来像这样的 jquery 函数

    function MonitorLoadStatus(loadId) {
     var url = 'LoadAdmin/GetLoadStatus/' + loadId;
     $.get(url, function (data) {
         if (data != "complete") {                 
             $("img[id =" + loadId + "]").show();
             window.setTimeout(function () {
                 MonitorLoadStatus(loadId);
             }, 1000);

         }
         else {
             $("img[id =" + loadId + "]").hide();
         };
     });
 }

还有一个看起来像这样的 MVC 方法

public ActionResult GetLoadStatus(string loadId)
    {
        // check some thing and return stuff
        return Content(currentProgress);
    }

上述方法的 loadid 从 jquery get 方法作为 null 传递。我到底做错了什么

【问题讨论】:

  • 您的 loadId 是否有效,您是否尝试过执行 console.log(loadId);在 $.get() 方法之前...?
  • 是的 loadId 在 $.get() 方法之前被正确传递

标签: asp.net-mvc jquery get


【解决方案1】:

确保您的 Global.asax 中有一条路线,其末尾有一个 /{loadId}。我提醒您,默认路由如下所示:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

意味着在你的控制器动作中参数应该被称为id

public ActionResult GetLoadStatus(string id)
{
    // check some thing and return stuff
    return Content(currentProgress);
}

如果您想使用loadId,请相应地更新您的路由定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多