2017-01-03 85 views
-2

我想我的MVC应用程序,我想在我看来做一个不错的文件上传。我的控制器是这样的,到目前为止:MVC文件上传

public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model) 
     { 
      ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2(); 
      model.groups = entities.Groups.ToList(); 
      model.users = entities.Users.ToList(); 
      if(ModelState.IsValid) 
      { 

      } 
      else 
      { 
       model.subject_id = model.subject_id; 
       model.groups = model.groups; 
       model.users = model.users; 
       return View(model); 
      } 
      return View(model); 

     } 

在我看来,我想作一个文件上传,这样我可以在if(ModelState.IsValid检索,然后上传它作为服务器的文件。我该怎么做呢?
编辑:
所以我说这个我的看法:

<div class="form-group"> 
        @Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         <input type="file" name="file" /> 
        </div> 
</div> 

但我怎么通过选择文件,以在模型中定义的HttpPostedFileBase file
EDIT2: 我的观点,如果现在是这样的:

@model ClassDeclarationsThsesis.Models.PickGroupForHomeworkViewModel 

@{ 
    ViewBag.Title = "Pick Group For Homework"; 
} 

<h2>Setting homework</h2> 

@foreach (var user in Model.users) 
{ 
    if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name) 
    { 
     if (user.user_type.Replace(" ", String.Empty) == 2.ToString()|| user.user_type.Replace(" ", String.Empty) == 3.ToString()) 
     { 
      using (Html.BeginForm("PickGroupForHomework", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
      { 
       @Html.AntiForgeryToken() 
       <hr /> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.deadline, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         @Html.TextBoxFor(m => m.deadline, new { @class = "form-control" }) 
        </div> 
       </div> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         <div class="editor-field"> 

          @Html.EditorFor(m=>m.file, new { @class="col-md-2 control-label"}) 
          @Html.ValidationMessageFor(m=>m.file) 
         </div> 
        </div> 
       </div> 


       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <input type="submit" class="btn btn-default" value="Submit" /> 
        </div> 
       </div> 
          } 
         } 
         if (user.user_type.Replace(" ", String.Empty) == 1.ToString()) 
         { 
          <p>You do not have enough permissions to enter this page. Contact the administrator.</p> 
           } 

          } 
         } 

而显然这EditorFor导致这样的观点:
enter image description here

+0

'ModelState.IsValid'返回TRUE或'FALSE'。您无法从中检索文件。 – Shyju

+0

当然我不能从这里检索它。但是如果一个模型是有效的,我可以从模型中检索它。 @Shyju –

+1

[File Upload ASP.NET MVC 3.0]可能的重复(http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0) – Kamo

回答

1

对于文件上载从形式工作,形式应该有 将值设置为“multipart/form-data”的enctype属性

使用此代码

@using (Html.BeginForm("PickGroupForHomework", "Account", FormMethod.Post, 
       new { @class = "form-horizontal",enctype = "multipart/form-data" })) 
{ 

    <input type="file" name="file" /> 
    <input type="submit" /> 
} 

这将生成的HTML标记为form标签设置enctype属性值为 “multipart/form-data的”