javascript - How to append whole set of model to formdata and obtain it in MVC -
how pass whole set model object through formdata , convert model type in controller?
below i've tried!
javascript part:
model = { eventfromdate: fromdate, eventtodate: todate, imageurl: imgurl, hotnewsdesc: $("#txthtdescription").val().trim(), }; formdata.append("model",model);
then pass through ajax, string, , if check value of request.form["model"]
result same, received string , value "[object object]"
is there way pass model through formdata , receive in controller?
if view based on model , have generated controls inside <form>
tags, can serialize model formdata
using
var formdata = new formdata($('form').get(0));
this include files generated <input type="file" name="myimage" .../>
and post using
$.ajax({ url: '@url.action("youractionname", "yourcontrollername")', type: 'post', data: formdata, processdata: false, contenttype: false, });
and in controller
[httppost] public actionresult youractionname(yourmodeltype model) { }
or (if model not include property httppostedfilebase
)
[httppost] public actionresult youractionname(yourmodeltype model, httppostedfilebase myimage) { }
if want add additional information not in form, can append using
formdata.append('someproperty', 'somevalue');
Comments
Post a Comment