jquery - how to send stringified Json from view to controller in MVC4 -
i want send stringified json object on button click. trying follows... on button click able call required action method, it's passing null
parameter..
memberlogin.cshtml
<input type="button" value="» continue" id="btnlogin" onclick="postdata();"> <script type="text/javascript"> function postdata() { var objlogindto = new object(); objlogindto.username = $("#txtusername").val(); objlogindto.password = $("#txtmemberpassword").val(); objlogindto.captchatext = $("#txtcaptcha").val(); var stringifieddata = json.stringify(objlogindto); $.ajax({ url: '@url.content("~/member/memberlogin/memberlogin")', async: false, type: "post", data: stringifieddata, datatype: "json", contenttype: "application/json; charset=utf-8", error: function (jqxhr, textstatus, errorthrown) { alert("error:" + jqxhr + "-" + textstatus + "-" + errorthrown); }, success: function (data, textstatus, jqxhr) { alert("success:" + data.message); } }); } </script>
action method
[httppost] public actionresult memberlogin(string jsonstr) { var objmemberlogindto = new javascriptserializer().deserialize<memberlogindto>(jsonstr); }
i getting null jsonstr
. note:i have included json2.js. stringifieddata
in ajax call holds stringified data. {"username":"test13361","password":"testpswd","captchatext":"gz8h4"}
'contenttype' property in $.ajax(...) config having problem. either remove contenttype set default or set proper content type. can find more on content type here
also @jai suggested, need wrap "data" in ajax(...) setting key-value pair key same of action method's parameter name proper modal-binding.
Comments
Post a Comment