2016-03-30 18 views
3

için kullanmak: Index.cshtml itibarenSağ yolu ben MVC4 uygulama yapısı şu var ajax

application structure
i SomeActionMethod() için ajax arama yapmaya çalışıyorum, yer benim

: benziyor

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="Scripts/jquery-2.2.2.min.js"></script> 
</head> 
<body> 
    <script> 
    $.ajax({ 
     url: '@Url.Action("SomeActionMethod", "HomeController")', 
     type: "POST", 
     cache: false, 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     success: function (data) { 
      alert(data); 
     }, 
     error: function() { 
      alert("fail"); 
     } 
    }); 
    </script> 
</body> 
</html> 

HomeController.cs: gibi HomeController.cs

Index.cshtml görünüyor enter image description here

yolu veya denetleyici nesi var:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace SapRepLanciatoreCAS.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     //trying to invoke from ajax 
     [HttpPost] 
     public ActionResult SomeActionMethod() 
     { 
      return Json(new { foo = "bar", baz = "Blech" }); 
     } 
    } 
} 

0 Ben bu hata var başvurumu başlattığınızda?

cevap

5

MVC, controller adının ilk kısmını göz önünde bulundurarak i.e. disregards Controller anahtar sözcüğünü alır (bkz. Home). Bu Size Yardımcı olacak

url: '@Url.Action("SomeActionMethod", "Home")', 
+0

Dostum! Haklısın! Ben bu sorun üzerine bir gün geçirdim ... –

+1

Bu sinir bozucu olduğunu biliyorum, Resharper gibi araçları deneyin ve bu gibi görüntüleme hataları gösterir;) – Zaki

+0

ASP.NET MVC Bu desen bana hata veriyor. – Graham

0
url:'@Url("Method Name","Controller Name")' 

Umut: HomeControllerHome değiştirmek - Yani url olmalıdır.