asp.net mvc - MVC3 (Razor) Json Get deserialized data in the Controller -
i need again asp.net mvc (razor view engine). in view (index) have
@model ienumerable<movie> i want pass model controller: stringify model in way:
<script type="text/javascript"> function postdata(){ var urlact='@url.action("createdoc")'; var model ='@html.raw(json.encode(model)); $.ajax({ data: json.stringify(model), type:"post", url:urlact, datatype:"json", contenttype:"application/json; charset=utf-8" }); } </script> everything seems work, cause know stringified data is:
'[{"id":1,"title":"the lord of rings: fellowship of ring","releasedate":"\/date(1007938800000)\/","genre":"fantasy","price":93000000.00},{"id":2,"title":"the lord of rings: 2 towers","releasedate":"\/date(1039042800000)\/","genre":"fantasy","price":94000000.00},{"id":3,"title":"the lord of rings: return of king","releasedate":"\/date(1070233200000)\/","genre":"fantasy","price":94000000.00}]'; the problem is: in controller, methodname "createdoc" (as declared in script) cannot access stringified data. following samples founded on web, method this:
[httppost] public actionresult createdoc(ienumerable<movie> movies) { //...using movies list return view(); } why can't access stringified data? how can it, there method call deserialize model in controller method? also, can use serialize() method instead of stringify() one? if so, what's syntax, view & controller side?
thank you.
you've got stringified data in javascript variable model. need call json.stringify on objects aren't strings.
if want post data action without modifying model, pass in model without json.stringify , modelbinder take care of deserializing ienumerable<movie> you.
if want use model other string, you'll want call json.parse on string. in other words:
var model = json.parse('@html.raw(json.encode(model))'); then, you'd want keep json.stringify call.
finally, stringify method on browser-specific object json, whereas serialize method on asp.net mvc-specific json object. 2 different worlds, same name.
Comments
Post a Comment