4

I have following Ajax.BeginForm on viewpage

using (Ajax.BeginForm("Financing_Product_Feature_Upload", "FileUpload", new { productid = @ViewBag.Product_ID }, new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data"}))
{
    @Html.AntiForgeryToken()
    <input type="file" name="files">   <input type="submit" value="Upload File to Server">
}

then I have following controller method in FileUpload controller class

[HttpPost]
public ActionResult Financing_Product_Feature_Upload(HttpPostedFileBase file, string productid)
{

but once I click above submit button its not directing to Financing_Product_Feature_Upload controller method

2
  • 3
    tried to rename the parameter HttpPostedFileBase file to HttpPostedFileBase files? in this way it matches the name of the input tag Commented Mar 24, 2016 at 11:34
  • Side note: You cannot upload files using Ajax.BeginForm() so your controller will never receive any files even if you do correct the typos. Commented Mar 24, 2016 at 21:12

3 Answers 3

3

MVC serialization works on name attributes. Name of the form control needs to match with MVC controller action parameters. In your case, I guess, you should be getting an error in browser console when you hit "Submit" button saying "there is no matching action found on contoller FileUpload" or something which gives that meaning.

@using (Ajax.BeginForm("Financing_Product_Feature_Upload", "FileUpload", new { productid = @ViewBag.Product_ID }, new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <input type="file" name="files">   <input type="submit" value="Upload File to Server">
}

public class FileUploadController : Controller
{
    [HttpPost]
    public ActionResult Financing_Product_Feature_Upload(HttpPostedFileBase  files, string productid)
    { 
        // Action code goes here
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

try add @ in enctype

using (Ajax.BeginForm("Financing_Product_Feature_Upload", "FileUpload", new { productid = @ViewBag.Product_ID }, new AjaxOptions() { HttpMethod = "POST" }, new { @enctype = "multipart/form-data"}))
                        {
                            @Html.AntiForgeryToken()
                            <input type="file" name="file">   <input type="submit" value="Upload File to Server">
                        }

Comments

0

Add @ before using:

@using (Ajax.BeginForm("Financing_Product_Feature_Upload", "FileUpload", new { productid = ViewBag.Product_ID }, new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data"}))
                        {
                            @Html.AntiForgeryToken()
                            <input type="file" name="files">   <input type="submit" value="Upload File to Server">
                        }

And rename the HttpPostedFileBase file to files because this is your file input name.

2 Comments

once I added @ front of using Unexpected "using" keyword after "@" character. Once inside code, you do not need to prefix constructs like "using" with "@".
@kez you have this using inside other code block and there you use @. Ex: @if(cond){ using(your).... and this is the reason of your exception and then you don't need @ before using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.