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

1
  • 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 Commented Dec 9, 2016 at 3:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks for your response , tried your code (copy paste) still getting null.
Check it name attribute and try different view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.