MVC4 Ajax call does not reach controller method
-
I am new to MVC4. I am trying to issue ajax call on the button click to receive partial view with the loaded dropdown list. It looks like Ajax call never reaches controller's method GetMyList(). What is wrong with my code ? Thank you //in mypage.cshtml @model IEnumerable @{ ViewBag.Title = "MyPage"; Layout = "~/Views/Shared/_MyLayout.cshtml"; }
....
Find
@Html.Partial("_MyPartialView", Model) // displayed on initial page load
...
@section MySection{ $('#btnGetList').on('click', function (event) { alert("go get list"); // shows up var myName = "Jo"; $.ajax({ type: "POST", url: "Main/GetMyList", data: myName, success: function (data) { alert("list is here"); //does Not show up $("#divWithPartialView").html(data); } }); alert("after ajax"); //shows up }) } // Controller namespace MyProject.Controllers { public class MainController: Controller { ............ [HttpGet] public ActionResult MyPage() { List myList = new List(); return View(myList); } public ActionResult GetMyList(string myName) { MyModel mm = new MyModel(); List myList = mm.GetNames(myName); // using search criteria entered on the previous Application page return PartialView("_MyPartialView", myList); } } } //Partial View: _MyPartialView.cshtml @model IEnumerable Select your name: - select name - @foreach (var name in Model) { @name.myName } //Model public class MyModel { public int id { get; private set; } public string myName { get; set; } public List GetNames(strin
-
I am new to MVC4. I am trying to issue ajax call on the button click to receive partial view with the loaded dropdown list. It looks like Ajax call never reaches controller's method GetMyList(). What is wrong with my code ? Thank you //in mypage.cshtml @model IEnumerable @{ ViewBag.Title = "MyPage"; Layout = "~/Views/Shared/_MyLayout.cshtml"; }
....
Find
@Html.Partial("_MyPartialView", Model) // displayed on initial page load
...
@section MySection{ $('#btnGetList').on('click', function (event) { alert("go get list"); // shows up var myName = "Jo"; $.ajax({ type: "POST", url: "Main/GetMyList", data: myName, success: function (data) { alert("list is here"); //does Not show up $("#divWithPartialView").html(data); } }); alert("after ajax"); //shows up }) } // Controller namespace MyProject.Controllers { public class MainController: Controller { ............ [HttpGet] public ActionResult MyPage() { List myList = new List(); return View(myList); } public ActionResult GetMyList(string myName) { MyModel mm = new MyModel(); List myList = mm.GetNames(myName); // using search criteria entered on the previous Application page return PartialView("_MyPartialView", myList); } } } //Partial View: _MyPartialView.cshtml @model IEnumerable Select your name: - select name - @foreach (var name in Model) { @name.myName } //Model public class MyModel { public int id { get; private set; } public string myName { get; set; } public List GetNames(strin
You have posted the same question twice: http://www.codeproject.com/Messages/4692608/MVC4-Ajax-call-does-not-reach-controller-method.aspx[^] http://www.codeproject.com/Messages/4692604/MVC4-Ajax-Call-does-not-reach-controller.aspx[^] Read this[^], and pay attention to point 9:
do not post the same question in more than one forum
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I am new to MVC4. I am trying to issue ajax call on the button click to receive partial view with the loaded dropdown list. It looks like Ajax call never reaches controller's method GetMyList(). What is wrong with my code ? Thank you //in mypage.cshtml @model IEnumerable @{ ViewBag.Title = "MyPage"; Layout = "~/Views/Shared/_MyLayout.cshtml"; }
....
Find
@Html.Partial("_MyPartialView", Model) // displayed on initial page load
...
@section MySection{ $('#btnGetList').on('click', function (event) { alert("go get list"); // shows up var myName = "Jo"; $.ajax({ type: "POST", url: "Main/GetMyList", data: myName, success: function (data) { alert("list is here"); //does Not show up $("#divWithPartialView").html(data); } }); alert("after ajax"); //shows up }) } // Controller namespace MyProject.Controllers { public class MainController: Controller { ............ [HttpGet] public ActionResult MyPage() { List myList = new List(); return View(myList); } public ActionResult GetMyList(string myName) { MyModel mm = new MyModel(); List myList = mm.GetNames(myName); // using search criteria entered on the previous Application page return PartialView("_MyPartialView", myList); } } } //Partial View: _MyPartialView.cshtml @model IEnumerable Select your name: - select name - @foreach (var name in Model) { @name.myName } //Model public class MyModel { public int id { get; private set; } public string myName { get; set; } public List GetNames(strin
Try...
$.ajax({
...
...
url: '@Url.Action("ActionName","ControllerName")',
...
...
})Pratik Bhuva --------------- The night is darkest just before the dawn. And I promise you, the dawn is coming