简体   繁体   English

将ASP.NET MVC Razor表单转换为EF创建带有文件上传的表单

[英]ASP.NET MVC Razor form to EF Create form with file upload

I've found numerous simple examples of uploading a file using an MVC Razor view and associated controller method but nine that include paying an EF model as well as the file back to the controller. 我发现了许多使用MVC Razor视图和关联的控制器方法上传文件的简单示例,但其中有9个示例,其中包括将EF模型以及文件返还给控制器。 Can anyone help with resources they have or have found? 任何人都可以帮助他们拥有或找到的资源吗?

Include a property in your view model: 在视图模型中包含一个属性:

class MyViewModel {
    public HttpPostedFileBase MyFile { get; set; }
    //other properties
}

Add the file input to your form: 将输入的文件添加到表单中:

<input type="file" name="myFile" />

Then the file will come in through your model: 然后文件将通过您的模型进入:

public ActionResult Create(MyViewModel model) {

    //model.MyFile will be myFile
}
 public ActionResult SomeControllerFunction(EFModel model, HttpPostedFileBase ImageFile)

where you have a form 您有表格的地方

@using (Html.BeginForm("SomeControllerFunction", "SomeController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    //various fields for the EF model, plus
     <input type="file" name="ImageFile" id="ImageFile" />
}

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

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