Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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(),
        };
formdata.append("model",model);

then pass it through ajax it will also be string and if I check the value of Request.Form["model"] 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?

share|improve this question
    
Didn't I just answer this here? – Stephen Muecke Mar 27 '15 at 5:05
    
I have dropped a comment @StephenMuecke. That's why I posted it here... – Guruprasad Rao Mar 27 '15 at 5:06
up vote 17 down vote accepted

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

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

and in your controller

[HttpPost]
public ActionResult YourActionName(YourModelType model)
{
}

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

[HttpPost]
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');
share|improve this answer
    
Thank you @StephenMuecke. That was perfect... :) – Guruprasad Rao Mar 27 '15 at 5:19
1  
@StephenMuecke Thank you very much. It is really one of the hardest issue regarding to Ajax call and with the help of you, it is solved :) I am wondering if it is also possible with Html.BeginForm and Ajax.BeginForm instead of <form>? If so, what changes should be applied? – binary Jun 20 '15 at 10:14
1  
Yes, it's possible with Html.BeginForm() to include files with a normal submit normal. Sorry, I'm a little lost. Which question was this in reference to? – Stephen Muecke Jun 21 '15 at 10:15
1  
@LuisGouveia, No its not with Ajax.BeginForm(). In any case the Ajax methods are obsolete (they are not even included in the latest version of MVC) and using $.ajax() (or its derivatives such as $.get(), $.load() etc give you much more flexibility. – Stephen Muecke Dec 11 '15 at 10:33
1  
@LuisGouveia, Yes thats another option, but why not just use FormData as per the answer? (or are you needing this for older browsers which dont support it?) – Stephen Muecke Dec 11 '15 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.