2012-11-26 55 views
0

我會先帶代碼。jquery Ajax Post調用錯誤的動作方法

控制器:

public ActionResult VideoList(int page) 
    { 
    } 
    [HttpPost] 
    public ActionResult SaveWindowWidth(string width) 
    { 
     return Json(new { success = true }); 
    } 

JS:

$(document).ready(function() 
{ 
var width = $("#Window").width(); 

$.ajax(
    { 
     type: "POST", 
     url: '/Video/SaveWindowWidth', 
     async: "true", 
     data: { "width": width }, 
     dataType: 'text json', 
     contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
     success: function (data, status) 
     { 
      if (data) 
      { 
       alert(data.status); 
      } 
     }, 
     error: function (data, status) 
     { 
      if (data) 
      { 
       alert(data.status); 
      } 
     } 
    }) 
}) 

途徑:即AJAX試圖調用VideoList方法

  routes.MapRoute(
      "Home", // Route name 
      "Home", // URL with parameters 
      new { controller = "Home", action = "Index" } 
     ); 

     routes.MapRoute(
      "BookPage", // Route name 
      "BookPage/{page}", // URL with parameters 
      new { controller = "Book", action = "Page" } 
     ); 

     routes.MapRoute(
      "BlogMember", // Route name 
      "BlogMember/{id}/{page}", // URL with parameters 
      new { controller = "Blog", action = "Member" } 
     ); 

     routes.MapRoute(
      "BlogPost", // Route name 
      "BlogPost/{id}/{page}", // URL with parameters 
      new { controller = "Blog", action = "Post" } 
     ); 

     routes.MapRoute(
      "BlogPage", // Route name 
      "BlogPage/{page}", // URL with parameters 
      new { controller = "Blog", action = "Page" } 
     ); 

     routes.MapRoute(
      "BlogTag", // Route name 
      "BlogTag/{tag}/{page}", // URL with parameters 
      new { controller = "Blog", action = "Tag" } 
     ); 

     routes.MapRoute(
      "NewBlogPostComment", // Route name 
      "NewBlogPostComment", // URL with parameters 
      new { controller = "Blog", action = "NewBlogPostComment" } 
     ); 

     routes.MapRoute(
      "NewBlogPost", // Route name 
      "NewBlogPost", // URL with parameters 
      new { controller = "Blog", action = "NewBlogPost" } 
     ); 

     routes.MapRoute(
      "EditBlogPost", // Route name 
      "EditBlogPost/{id}", // URL with parameters 
      new { controller = "Blog", action = "EditBlogPost" } 
     ); 

     routes.MapRoute(
      "Account", // Route name 
      "Account", // URL with parameters 
      new { controller = "Account", action = "Update" } 
     ); 

     routes.MapRoute(
      "LogOff", // Route name 
      "LogOff", // URL with parameters 
      new { controller = "Account", action = "LogOff" } 
     ); 

     routes.MapRoute(
      "LogOn", // Route name 
      "LogOn", // URL with parameters 
      new { controller = "Account", action = "LogOn" } 
     ); 

     routes.MapRoute(
      "Register", // Route name 
      "Register", // URL with parameters 
      new { controller = "Account", action = "Register" } 
     ); 

     //routes.MapRoute(
     // "About", // Route name 
     // "About", // URL with parameters 
     // new { controller = "Home", action = "About" } 
     //); 

     routes.MapRoute(
      "Contact", // Route name 
      "Contact", // URL with parameters 
      new { controller = "Home", action = "Contact" } 
     ); 

     routes.MapRoute(
      "UnderConstruction", // Route name 
      "UnderConstruction", // URL with parameters 
      new { controller = "Home", action = "UnderConstruction" } 
     ); 

     routes.MapRoute(
      "DisableBlogComment", // Route name 
      "DisableBlogComment/{id}", // URL with parameters 
      new { controller = "Blog", action = "DisableBlogComment" } 
     ); 

     routes.MapRoute(
      "DisableAllMemberBlogComments", // Route name 
      "DisableAllMemberBlogComments/{id}", // URL with parameters 
      new { controller = "Blog", action = "DisableAllMemberBlogComments" } 
     ); 

     routes.MapRoute(
      "DisableVideoComment", // Route name 
      "DisableVideoComment/{id}", // URL with parameters 
      new { controller = "Video", action = "DisableVideoComment" } 
     ); 

     routes.MapRoute(
      "DisableAllMemberVideoComments", // Route name 
      "DisableAllMemberVideoComments/{id}", // URL with parameters 
      new { controller = "Video", action = "DisableAllMemberVideoComments" } 
     ); 

     routes.MapRoute(
      "DisableMember", // Route name 
      "DisableMember/{id}", // URL with parameters 
      new { controller = "Member", action = "DisableMember" } 
     ); 

     routes.MapRoute(
      "NewPost", // Route name 
      "NewPost", // URL with parameters 
      new { controller = "Blog", action = "NewPost" } 
     ); 

     routes.MapRoute(
      "InactiveBlogPosts", // Route name 
      "InactiveBlogPosts/{page}", // URL with parameters 
      new { controller = "Blog", action = "InactiveBlogPosts" } 
     ); 

     routes.MapRoute(
      "InactiveBlogComments", // Route name 
      "InactiveBlogComments/{page}", // URL with parameters 
      new { controller = "Blog", action = "InactiveBlogComments" } 
     ); 

     routes.MapRoute(
      "InactiveVideoComments", // Route name 
      "InactiveVideoComments/{page}", // URL with parameters 
      new { controller = "Video", action = "InactiveVideoComments" } 
     ); 

     routes.MapRoute(
      "EditHomePage", // Route name 
      "EditHomePage", // URL with parameters 
      new { controller = "Home", action = "EditHomePage" } 
     ); 

     routes.MapRoute(
      "EditAboutPage", // Route name 
      "EditAboutPage", // URL with parameters 
      new { controller = "Home", action = "EditAboutPage" } 
     ); 

     routes.MapRoute(
      "Newsletter", // Route name 
      "Newsletter", // URL with parameters 
      new { controller = "Newsletter", action = "Newsletter" } 
     ); 

     routes.MapRoute(
      "RegisterNewVideo", // Route name 
      "RegisterNewVideo", // URL with parameters 
      new { controller = "Video", action = "RegisterNewVideo" } 
     ); 

     routes.MapRoute(
      "Members", // Route name 
      "Members/{page}", // URL with parameters 
      new { controller = "Member", action = "MemberList" } 
     ); 

     routes.MapRoute(
      "EditMember", // Route name 
      "EditMember/{id}", // URL with parameters 
      new { controller = "Member", action = "EditMember" } 
     ); 

     routes.MapRoute(
      "AppSettings", // Route name 
      "AppSettings", // URL with parameters 
      new { controller = "Utility", action = "AppSettings" } 
     ); 

     routes.MapRoute(
      "AudioBookPage", // Route name 
      "AudioBookPage/{page}", // URL with parameters 
      new { controller = "Book", action = "AudioBookPage" } 
     ); 

     routes.MapRoute(
      "IPBlocked", // Route name 
      "IPBlocked", // URL with parameters 
      new { controller = "Utility", action = "IPBlocked" } 
     ); 

     routes.MapRoute(
      "LiveTV", // Route name 
      "LiveTV", // URL with parameters 
      new { controller = "LiveTV", action = "LiveTV" } 
     ); 

     routes.MapRoute(
      "VideoPlayer", // Route name 
      "VideoPlayer/{id}/{page}", // URL with parameters 
      new { controller = "Video", action = "VideoPlayer" } 
     ); 

     routes.MapRoute(
      "Video", // Route name 
      "Video/{page}", // URL with parameters 
      new { controller = "Video", action = "VideoList" } 
     ); 

     routes.MapRoute(
      "NewVideoComment", // Route name 
      "NewVideoComment", // URL with parameters 
      new { controller = "Video", action = "NewVideoComment" } 
     ); 

     routes.MapRoute(
      "Music", // Route name 
      "Music", // URL with parameters 
      new { controller = "Music", action = "Index" } 
     ); 

     routes.MapRoute(
      "RootsCompilationOne", // Route name 
      "Music/RootsCompilationOne", // URL with parameters 
      new { controller = "Music", action = "RootsCompilationOne" } 
     ); 

     routes.MapRoute(
      "UnityIsStrength", // Route name 
      "Music/UnityIsStrength", // URL with parameters 
      new { controller = "Music", action = "UnityIsStrength" } 
     ); 

     routes.MapRoute(
      "FileUpload", // Route name 
      "FileUpload", // URL with parameters 
      new { controller = "Utility", action = "FileUpload" } 
     ); 

     routes.MapRoute(
      "PageUnavailable", // Route name 
      "PageUnavailable", // URL with parameters 
      new { controller = "Utility", action = "PageUnavailable" } 
     ); 

     routes.MapRoute(
      "VideoGrid", // Route name 
      "VideoGrid/{page}", // URL with parameters 
      new { controller = "Video", action = "VideoGrid" } 
     ); 

     routes.MapRoute(
      "EditVideo", // Route name 
      "EditVideo/{id}", // URL with parameters 
      new { controller = "Video", action = "EditVideo" } 
     ); 

     routes.MapRoute(
      "CopyVideo", // Route name 
      "CopyVideo/{id}/{page}", // URL with parameters 
      new { controller = "Video", action = "CopyVideo" } 
     ); 

     routes.MapRoute(
      "DeleteIPLogs", // Route name 
      "DeleteIPLogs/{ip}", // URL with parameters 
      new { controller = "Utility", action = "DeleteIPLogs" } 
     ); 

     routes.MapRoute(
      "SaveWindowWidth", // Route name 
      "SaveWindowWidth/{width}", // URL with parameters 
      new { controller = "Video", action = "SaveWindowWidth" } 
     ); 

     routes.MapRoute(
      "FacebookLikes", // Route name 
      "FacebookLikes", // URL with parameters 
      new { controller = "Utility", action = "FacebookLikes" } 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

問題。我看到它時讀取響應:

參數字典包含參數「頁」非空類型「System.Int32」的用於方法的空條目「System.Web.Mvc.ActionResult VideoList(Int32)已」

我聽說某處它可能是路由有問題,所以我也在這裏發佈了它們。 我會感激你的幫助

問候 的Mariusz

+0

我忘了添加。嘗試從視圖中使用@ Ajax.RouteLink時會發生同樣的問題。 – Mariusz

回答

3

您的視頻路由沒有配置好。 MVC嘗試從每個url獲取控制器和操作,從適當的控制器中找出所需的方法。而在你的情況下,總是得到相同的值VideoController.VideoList():

routes.MapRoute(
      "Video", // Route name 
      "Video/{page}", // URL with parameters 
      new { controller = "Video", action = "VideoList" } 
     ); 

如果你改變這個路線,一切都會好起來:

routes.MapRoute(
      "Video", // Route name 
      "Video/{action}", // URL with parameters 
      new { controller = "Video", action = "VideoList" } 
     ); 
+0

謝謝Dim。是的,它起到了魅力的作用。謝謝:-) – Mariusz

1

使用字符串存儲值作爲URL不是最佳做法。

我這樣做。

查看

@Html.Hidden("getVideoUrl", Url.Action("SaveWindowWidth", "Video")) 

JS

var url = $("#getVideoUrl").val(); 
    $.post(url, data: { "width": width },  
     success: function (data, status) 
     { 
      if (data) 
      { 
       alert(data.status); 
      } 
     }, 
     error: function (data, status) 
     { 
      if (data) 
      { 
       alert(data.status); 
      } 
     }); 
+0

非常好的建議。將以這種方式開始使用它。 這種方法比不創建依賴關係更安全。尼斯。 MVC還是新手:-) – Mariusz

1

迪馬已經確定了問題,雖然他的解決方案是不適合你相當的工作。您的路由配置爲在請求url Video/SaveWindowWidth時,請在控制器Video上調用動作VideoList,並將參數page設置爲SaveWindowWidth。顯然這不是你想要的。

ASP.Net MVC將執行匹配的第一條路由。這就是爲什麼首先包含所有特定路線的原因。

解決此問題的方法之一是重新編寫路由,以便某些API路由配置在通用漂亮路由之上。爲此,您需要在任何漂亮的網址之前提及每個組合。

例如,

routes.MapRoute(
     "SaveWindowWidthForVideo", // Route name 
     "Video/SaveWindowWidth", 
     new { controller = "Video", action = "SaveWindowWidth" } 
    ); 

routes.MapRoute(
     "SaveWindowHeightForVideo", // Route name 
     "Video/SaveWindowHeight", 
     new { controller = "Video", action = "SaveWindowHeight" } 
    ); 

routes.MapRoute(
     "SaveWindowSomethingElseForVideo", // Route name 
     "Video/SaveWindowHeight", 
     new { controller = "Video", action = "SaveWindowSomethingElseForVideo" } 
    ); 

// etc etc  

// your remaining pretty urls 
routes.MapRoute(
     "Video", // Route name 
     "Video/{page}", // URL with parameters 
     new { controller = "Video", action = "VideoList" } 
    ); 

我不喜歡這樣做,因爲它可以得到相當麻煩很容易。

我更喜歡的解決方案是通過在url中添加單詞api來區分我漂亮的url路由和我的ajax API路由。

routes.MapRoute(
     "ApiCallsForVideo", // Route name 
     "api/video/{action}", 
     new { controller = "Video" } 
    ); 

// or 

routes.MapRoute(
     "ApiCalls", // Route name 
     "api/{controller}/{action}" 
    ); 



// etc etc  

routes.MapRoute(
     "Video", // Route name 
     "Video/{page}", // URL with parameters 
     new { controller = "Video", action = "VideoList" } 
    ); 

這顯然涉及到修改你的Ajax調用,包括字api

+0

非常好。從中學到很多,並將實施。迪馬斯不是直接幫助工作,而是讓我意識到我需要重新配置路線。 感謝您的建議。 – Mariusz

0

嗨,我認爲你需要在路由中添加參數,然後只有你的ajax將調用參數的方法。現在發生了什麼是AJAX烏爾正在調用該特定methot但你的路線已經沒有提到允許參數

routes.MapRoute(
      "Video", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Video", action = "VideoList", width= UrlParameter.Optional } // Parameter defaults 
     ); 

那麼只有UR參數可以傳遞給UR控制器。 並調用URL看起來像這樣

url: '/Video/SaveWindowWidth'+width, 

url: '/Video/SaveWindowWidth?width=' + width, 

其工作對我很好!

希望它有幫助