Guruprasad Rao Guruprasad Rao - 1 year ago 124
Ajax Question

how to append whole set of model to formdata and obtain it in MVC

how to pass whole set model object through formdata and convert it to
model type in controller.

Below is how I've tried till now!

my js part:

model = {
EventFromDate: fromDate,
EventToDate: toDate,
ImageUrl: imgUrl,
HotNewsDesc: $("#txthtDescription").val().trim(),

then pass it through ajax it will also be string and if I check the value of
the result will be same, I mean it will be received as string and value will be
"[object object]"

Is there any way to pass model through formdata and receive it in controller?

Answer Source

If your view is based on a model and you have generated the controls inside <form> tags, then you can serialize the model to FormData using

var formdata = new FormData($('form').get(0));

This will also include any files generated with <input type="file" name="myImage" .../>

and post it back using

  url: '@Url.Action("YourActionName", "YourControllerName")',
  type: 'POST',
  data: formdata,
  processData: false,
  contentType: false,         

and in your controller

public ActionResult YourActionName(YourModelType model)

or (if your model does not include a property for HttpPostedFileBase)

public ActionResult YourActionName(YourModelType model, HttpPostedFileBase myImage)

If you want to add additional information that is not in the form, then you can append it using

formdata.append('someProperty', 'SomeValue');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download