asp.net mvc - How to parse object (and its elements) from ajax request -
here's issue. i'm making ajax request obtain object controller. object (or something) being brought back, don't know how access attributes of object being brought back. object of type "address" , has attributes address.address1, address.city, etc. here code: after button clicked,
function showeditaddress(addressid) { $.get("/website/accommodation/addressget", { guid: addressid.tostring() }, function(data) { //get values variable 'data' such described above //and append form 'dialog' $("#dialog").dialog({ // autoopen: false, show: { effect: "explode", duration: 250 }, hide: { effect: "explode", duration: 250 }, buttons: { "save": { text: "save", class: "", click: function () { //save form $(this).dialog("close"); } }, "cancel": { text: "cancel", class: "", click: function () { $(this).dialog("close"); } } }, modal: true }); }); }
controller action:
public address addressget(string guid) { guid g = new guid(guid); return _icorerepository.addresses.where(p => p.addressid == g).singleordefault(); }
any appreciated!!! thank you!!!
jose quite simple. answer question. value of properties of address
need put dot in front of data
, type de property name. this:
//(....) function(data) { //get values variable 'data' such described above //and append form 'dialog' //show address id. alert(data.addressid);
remember javascript case sensitive, need use upper case a
, id
in addressid did in c#.
and in controller need replace last line this:
var address = _icorerepository.addresses.where(p => p.addressid == g).singleordefault(); return json(address, jsonrequestbehavior.allowget);
the method must return jsonresult
. method json serialize object, in case address, in json format before response data client.
if need json method accepts ienumerables to, list<> or array. in javascript data object lenght property , acess each element using indexer like:
data[0].addressid.
Comments
Post a Comment