简体   繁体   中英

Ajax.BeginForm send parameters to controller

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM