Ajax.BeginForm将参数发送到控制器

时间:2016-03-24 11:12:50

标签: c# jquery ajax asp.net-mvc

我在浏览页面上关注Ajax.BeginForm

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

然后我在FileUpload控制器类

中有以下控制器方法
[HttpPost]
public ActionResult Financing_Product_Feature_Upload(HttpPostedFileBase file, string productid)
{

但是一旦我单击上面的提交按钮,它就不会指向Financing_Product_Feature_Upload控制器方法

3 个答案:

答案 0 :(得分:1)

MVC序列化适用于名称属性。表单控件的名称需要与MVC控制器操作参数匹配。在你的情况下,我想,当你点击“提交”按钮说“在控制器FileUpload上找不到匹配的动作”或者说具有这种意义的东西时,你应该在浏览器控制台中出错。

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

答案 1 :(得分:0)

尝试在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">
                        }

答案 2 :(得分:0)

使用前添加@

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

并将HttpPostedFileBase file重命名为files,因为这是您的文件输入名称。