jquery - Javascript and MVC return types -
i have javascript in page of asp.net mvc project:
function showalliancemembers_onclick() { var strname = $("#alliancenametextbox").val(); $.ajax( { type: "post", url: "/alliance/index", data: "alliancename=" + strname, success: function (result) { if (result.success) { alert("done!"); } else { alert("error!!"); } }, error: function (req, status, error) { alert(error); } }); }
as know, script calling mvc action
. here mvc action
:
[httppost] public actionresult index(string alliancename) { //populating object containing list (ilist) return view(res); }
the problem here javascript code shows alert error message... what's wrong in code?
in controller action not sending json, simple view. there no .success
property defined on result
variable. here's how ajax request like:
$.ajax({ type: "post", url: "/alliance/index", data: { alliancename: strname }, success: function (result) { // if got far ajax request succeeded, result variable // contain final html of rendered view alert("done!"); }, error: function (req, status, error) { alert(error); } });
or if want send json controller action:
[httppost] public actionresult index(string alliancename) { // populate result return json(new { success = true, result = res }); }
and then:
$.ajax({ type: "post", url: "/alliance/index", data: { alliancename: strname }, success: function (result) { if (result.success) { // result.res alert("done!"); } }, error: function (req, status, error) { alert(error); } });
Comments
Post a Comment