c# - How do I create a model using AJAX in ASP.NET MVC 5 -


currently can create models regularly (by going settingprofile/create)

but when try use ajax send data wrapped in settingprofile js object returns http 500 internal server error error, believe problem in data type. correct way of calling create method in ajax?

my code:

model:

    public class settingprofile     {         [databasegenerated(databasegeneratedoption.identity)]         public int id { get; set; }         public string name { get; set; }         public long? userid { get; set; }         public string url { get; set; }     } 

view (js):

function savesettingprofile() {         var name = prompt("please enter profile name", "");         var url = $("form").serialize(); // not ajax url, variable in model          var settingprofile = {             name: name,             url: url         };          jquery.ajax({             url: "@url.action("create", "settingprofile")",             contenttype: "application/json; charset=utf-8",             datatype: "json",             method: "post",             data: settingprofile         }).done(function (response) {             alert("profile saved successfully");         }).fail(function () {             alert("could not save profile");         });     } 

controller:

        [httppost]         //[validateantiforgerytoken]         public actionresult create([bind(include = "name,url")] settingprofile settingprofile)         {             settingprofile.userid = 8; // todo: user id session             if (modelstate.isvalid)             {                 db.settingprofiles.add(settingprofile);                 db.savechanges();                 return json(new { success = true });             }             return json(new { success = false });         } 

500 error means, server code crashing. of many reasons. 1 thing noticed (which might cause 500 error) way sending data.

you specified contenttype "application/json" .but sending javascript object is. request payload sent like

name=somenamevalue&url=someurlvalue , current server code, model binder cannot map object of settingprofile.

the solution send stringified version of javascript object. may use json.stringify method so.

 jquery.ajax({                 url: "@url.action("create", "settingprofile")",                 contenttype: "application/json; charset=utf-8",                 method: "post",                 data: json.stringify(settingprofile)         }).done(function (response) {             alert("profile saved successfully");         }; 

now client code send request payload like

{"name":"somename","url":"someurl"} 

and model binder able map properly.

if simple data, can not mention custom contenttype , send js object is. jquery use default contenttype value "application/x-www-form-urlencoded" , model binding work.

so below code work fine.

  jquery.ajax({                 url: "@url.action("create", "settingprofile")",                 method: "post",                 data:settingprofile         }) 

you may check browser's network tab , see response coming server ajax call. if exception happens, can see details there.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -