0

I've trying to send a file through Ajax.beginForm with enctype = "multipart/form-data", but I always get it as null,

I'm returning a JavaScriptResult from My controller like this,

public JavaScriptResult SaveFile(MyModel model)
{
    JavaScriptResult result = new JavaScriptResult();
    result.Script = "$('#ShowPopup').trigger('click')";

    return result;
}

When using Html.BeginForm it works great, but when it returns the result, it shows the following on the browser,

I don't know what's wrong with Html.BeginForm(), also when I'm returning a view, the view returns but without the my _Layout.cshtml page, breaks my design,

so the problem is when I use ajax it perfectly great but doesn't post file, and when I'm using html.beginForm, it posts the file, but breaks the design after return.

Ibraheem Al-Saady
  • 854
  • 3
  • 14
  • 30
  • you want to upload a file through ajax? – Abbas Galiyakotwala Apr 15 '15 at 06:31
  • What properties does your `MyModel` contains?? – Guruprasad J Rao Apr 15 '15 at 06:32
  • 3
    If you want to post using ajax, you need to use `FormData` as shown in [this example](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681). And then you can just call `"$('#ShowPopup').trigger('click')";` in the success callback rather than trying to return a script. –  Apr 15 '15 at 06:33
  • You can use an Html.BeginForm and in java script convert it to ajax request in javascript using [jquery form plugin](http://malsup.com/jquery/form/). I use it and it works great. Please look at the questions [here](http://stackoverflow.com/questions/17037948/not-able-to-upload-file-using-ajax-beginform-asynchronously) and [here](http://stackoverflow.com/questions/19042116/ajax-beginform-in-mvc-to-upload-files) – Mansoor Apr 15 '15 at 06:40

0 Answers0