简体   繁体   中英

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

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