asp.net mvc - Pass filled MVC viewmodel to a controller action via jquery AJAX call -
i have page through action method, fills complex view model has lots of properties , few list objects.
var model = new selecttargetingviewmodel(signupservice, ordersignupitem, creativetemplateid, culturename, false, dealermedias.tolist());
i filling datatable via ajax call right calls same code above.
[httppost] [route("~/selecttargeting/getdealermediarates")] public actionresult getdealermediarates(int ordersignupitemid, int? creativetemplateid) { appcontext.menuaction = "dealermediaindex"; appcontext.menucontroller = "signup"; string culturename = "en"; appcontext.setmenuparametersfromcontext(); var ordersignupitem = signupservice.getordersignupitembyid(ordersignupitemid); var dealermedias = signupservice.getdealermedias(ordersignupitem.ordersignupdetail.order.dealerid, ordersignupitem.ordersignupdetail.order.seasonid, ordersignupitem.packagemediatype.seasonmediatype.clientmediatypeid); var model = new selecttargetingviewmodel(signupservice, ordersignupitem, creativetemplateid, culturename, false, dealermedias.tolist()); return partialview("_getdealermediarate", model); }
then call method via ajax.
$.ajax({ url: '@url.action("getdealermediarates", "signup", new { culturename = model.currentculturename })?ordersignupitemid=@model.ordersignupitemid&creativetemplateid=@model.creativetemplateid', type: "post", datatype: "html", success: function (data) { var newhtml = $(data); $('.dealermediarates').html(newhtml); otable = $('#targetingtable').datatable({ 'aocolumndefs': [{ 'bsortable': false, 'atargets': ['nosort'] }], 'order': [[1, 'asc']], 'alengthmenu': [[10, 25, 50, 100, 150, -1], [10, 25, 50, 100, 150, "all"]], processing: true, "language": { "processing": "<div style='position: fixed; top: 50%; left: 50%; width: 250px; height: 80px; background-color: #fff; text-align: center; z-index: 10; outline: 0 0 0 9999px solid rgba(0, 0, 0, 0.5); border: 1px solid #aaa; padding: 10px 5px;'><i class='fa fa-spinner fa-spin'></i> processing...please wait</div>" } }); osettings = otable.settings(); $('.targetingarea').unblock(); } });
this working takes longer time run trying fill model twice. in process of optimizing process instead of filling main model twice, want fill once , pass ajax post method. have tried far model , pass model variable in method call when debug code, see in model looks default constructor, initialized using page action method, values not there anymore. first of all, possible take viewmodel , pass method of properties , values, if doing http post? missing in code below.
ajax call
var model = @html.raw(@jsonconvert.serializeobject(@model)); var datatosend = json.stringify(model); $.ajax({ url: '@url.action("getdealermediarates", "signup")', type: "post", data: datatosend, contenttype: "application/x-www-form-urlencoded; charset=utf-8", datatype: "html", success: function (data) { var newhtml = $(data); $('.dealermediarates').html(newhtml); otable = $('#targetingtable').datatable({ 'aocolumndefs': [{ 'bsortable': false, 'atargets': ['nosort'] }], 'order': [[1, 'asc']], 'alengthmenu': [[10, 25, 50, 100, 150, -1], [10, 25, 50, 100, 150, "all"]], processing: true, "language": { "processing": "<div style='position: fixed; top: 50%; left: 50%; width: 250px; height: 80px; background-color: #fff; text-align: center; z-index: 10; outline: 0 0 0 9999px solid rgba(0, 0, 0, 0.5); border: 1px solid #aaa; padding: 10px 5px;'><i class='fa fa-spinner fa-spin'></i> processing...please wait</div>" } }); osettings = otable.settings(); $('.targetingarea').unblock(); } });
get method on controller
[httppost] [route("~/selecttargeting/getdealermediarates")] public actionresult getdealermediarates(selecttargetingviewmodel model) { appcontext.menuaction = "dealermediaindex"; appcontext.menucontroller = "signup"; return partialview("_getdealermediarate", model); }
when debug , @ model below in post method, seeing model not null looks being initialized default constructor. if not possible, work towards different solution thought check if missing anything.
Comments
Post a Comment