Upload Images while creating new Model

前端 未结 3 813
孤独总比滥情好
孤独总比滥情好 2020-12-21 10:50

I\'m going to create profile for my users in ASP.Net MVC application. Users creation controller is something like this:

[HttpPost]
[ValidateAntiForgeryToken]         


        
3条回答
  •  清酒与你
    2020-12-21 11:15

    You need to use an BeginForm() that allows you to add htmlAttributes, and because you need to add new {enctype = "multipart/form-data" }

    @using (Html.BeginForm("UserProfileViewModel ", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    

    Controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UserProfileViewModel(UserProfileViewModel userViewModel)
    {
    if (ModelState.IsValid)
    {
      HttpPostedFileBase fileUpload= Request.Files[0];
      //for collection
      HttpFileCollectionBase fileUpload= Request.Files;
     ....
     }
    

提交回复
热议问题