Ajax.BeginForm send parameters to controller

16,253

Solution 1

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
    }
}

Solution 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">
                        }
Share:
16,253
kez
Author by

kez

Currently looking for Full Time or Part Time Job that can work Remotely

Updated on June 26, 2022

Comments

  • kez
    kez almost 2 years

    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