2017-01-03 17 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でそれを取得し、サーバー上のファイルとしてアップロードできるように、ファイルのアップロードをしたいと思います。これはどうすればいいですか?
EDIT:
だから私は、私の見解にこれを追加しました:

<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

"multipart/form-data" に設定さenctype属性値とformタグのHTMLマークアップを生成します。そこからファイルを取り出すことはできません。 – 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" /> 
} 

これは `ModelState.IsValid`が` true`を又は `false`のを返す

関連する問題