简体   繁体   English

Ajax.BeginForm 向控制器发送参数

[英]Ajax.BeginForm send parameters to controller

I have following Ajax.BeginForm on viewpage我在Ajax.BeginForm上关注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">
}

then I have following controller method in FileUpload controller class然后我在FileUpload控制器类中有以下控制器方法

[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但是一旦我点击上面的提交按钮, Financing_Product_Feature_Upload不会指向Financing_Product_Feature_Upload控制器方法

try add @ in enctype尝试在 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. MVC 序列化适用于名称属性。 Name of the form control needs to match with MVC controller action parameters.表单控件的名称需要与 MVC 控制器操作参数匹配。 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.在您的情况下,我想,当您点击“提交”按钮时,您应该在浏览器控制台中收到错误消息,说“在控制器 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
    }
}

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.并将HttpPostedFileBase file重命名为files因为这是您的文件输入名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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