0

I am trying to upload image using Ajax.BeginForm but in my controller the file shows null. My code is:

@using (Ajax.BeginForm("Create", "PriceManager", new AjaxOptions
            {
                HttpMethod = "Post",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "PriceList",
                OnSuccess = "ClearInputField",
                OnFailure = "Fail",
                OnComplete = "Complete"


            }, new { enctype= "multipart/form-data" }))

I even tried using Html.Begin :

 @{ 
        var attributes = new Dictionary<string, object>();


        attributes.Add("data-ajax-complete ", "Complete");
        attributes.Add("data-ajax-mode", "replace");
        attributes.Add("data-ajax-update ", "#PriceList");
        attributes.Add("id", "form0");
        attributes.Add("enctype", "multipart/form-data");
        attributes.Add("data-ajax", "true");
    }
@using (Html.BeginForm("Create", "ProductManager", FormMethod.Post,attributes))

Still the file value remain null. Any idea what I am doing wrong here. Thanks

Fahad
  • 173
  • 1
  • 12
  • It will work fine with a normal submit, but you cannot upload files using `Ajax.BeginForm()` (your 2nd attempt is just mimicking `Ajax.BeginForm()`). If you want to use ajax, then use `$.ajax()` with the correct options and passing `FormData` - refer [how to append whole set of model to formdata and obtain it in MVC](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Dec 09 '16 at 03:56

1 Answers1

2

Try this:

    @using (Ajax.BeginForm("About", "Home", new AjaxOptions
{
    HttpMethod = "Post",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "PriceList",
    OnSuccess = "ClearInputField",
    OnFailure = "Fail",
    OnComplete = "Complete"


}, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="flResim" />

    <input type="submit" value="send" />
}

I tried and worked.

hakantopuz
  • 481
  • 5
  • 11