c# - Unable to pass model from View to controler using ajax -
first of can search , found questions this: (passing model view controller using jquery ajax
but when i'm trying make in marker answer receive error circular reference
i'm asking in sending model view controler: here view:
@model magazyn.models.deviceusage @{ viewbag.title = "return"; //layout = "~/views/shared/_layout.cshtml"; var val = json.encode(model); } <h2>return</h2> <script> $(document).ready(function () { $('.z').on('click', function (event) { event.preventdefault(); var check=@html.raw(model) $.ajax({ url: "/deviceusage/return", type: "post", contenttype: "application/json; charset=utf-8", datatype: 'html', data: '{dev:' + json.stringify(check) + '}', error: function (data) { alert("wystąpił nieokreślony błąd " + data); }, success: function (data) { $('.modal-body').html(data); } }) }) }); </script> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>deviceusage</h4> <hr /> @html.validationsummary(true) <div class="input-group"> <div class="input-group-addon"> @html.labelfor(model => model.storageid, "gdzie oddałeś?", new { @class = "control-label col-md-2" }) </div> <div class=""> @html.dropdownlist("storageid", null, new {@class="form-control" }) @html.validationmessagefor(model => model.storageid) </div> </div> @html.hiddenfor(model => model.deviceinstanceid) <div class="input-group" style="padding-top:5px"> <input type="submit" value="save" class="btn btn-default z" /> </div> </div> }
and controller:
[httppost] [validateantiforgerytoken] public actionresult return(deviceusage dev) { if(dev.storageid==3) { modelstate.addmodelerror("", "nie można oddać na własne biurko"); viewbag.storageid = new selectlist(unitofwork.storagerepository.get(), "id", "name", dev.storageid); return partialview(dev); } dev.userid = 1; unitofwork.deviceusagerepository.update(dev); unitofwork.save(); return redirecttoaction("mydevices"); }
i'm asking suggestions, code snippets me in solving problem.
@update
with uncommented var val= json.encode(model);
error:
a circular reference detected while serializing object of type 'system.data.entity.dynamicproxies.deviceinstance_445e26162dd165393d1cb531a60ba066c466c22965b1714e8f224391398180ca'.
with commented uncaught referenceerror: system not defined
above error thrown when trying stringify
i doubt on following line of code:
var check=@html.raw(model) data: '{dev:' + json.stringify(check) + '}'
can try below code?
$.ajax({ url: "/deviceusage/return", type: "post", contenttype: "application/json; charset=utf-8", data: json.stringify({ dev: deviceinstanceid: $('#deviceinstanceid').val(), userid: "1", storageid: $('#storageid').val() }), error: function (data) { alert("wystąpił nieokreślony błąd " + data); }, success: function (data) { $('.modal-body').html(data); } });
Comments
Post a Comment