2017-11-04 35 views
-1

で常にnullです:ビューのための2つのモデルの一つは、私は以下のように2つのモデル<strong>注文</strong>と<strong>ファイル</strong>を持っている私のMVC5アプリケーションではMVC 5

public class Order 
{ 
    public int OrderID { get; set; } 
    public string OrderName{ get; set; } 
} 

public class File 
{ 
    public HttpPostedFileBase[] files { get; set; } 
} 

私は単一で両方のクラスの編集オブジェクトをしたいですビュー、私は親クラスを作成します。

:私はこれを持っているビューで

public class MainContext 
{ 
    public Order Order { get; set; } 
    public File File { get; set; } 
} 

@using (Html.BeginForm("Create", "Order", FormMethod.Post, new { encType = "multipart/form-data" })) 
@Html.AntiForgeryToken() 

<div class="form-group"> 
    <label>OrderName</label> 
    @Html.EditorFor(model => model.Order.OrderName, new { htmlAttributes = new { @class = "form-control" } }) 
    @Html.ValidationMessageFor(model => model.Order.OrderName, "", new { @class = "text-danger" }) 
</div> 

<div class="form-group"> 
    @Html.LabelFor(model => model.File.files, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(model => model.File.files, "", new { @type = "file", @multiple = "multiple", }) 
     @Html.ValidationMessageFor(model => model.File.files, "", new { @class = "text-danger" }) 
    </div> 
</div> 

<div class="form-group"> 
    <input type="submit" value="submit" class="btn btn-success btn-lg btn-block" /> 
</div> 

コントローラ

public ActionResult Create() 
{ 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "OrderName")] Order order, HttpPostedFileBase[] files) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Order.Add(order); 
     db.SaveChanges(); 

     if (files != null) 
     { 
      foreach (HttpPostedFileBase file in files) 
      { 
       if (file != null) 
       { 
        var InputFileName = Path.GetFileName(file.FileName); 
        var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + InputFileName); 
        file.SaveAs(ServerSavePath); 
       } 
      } 
     } 
     return RedirectToAction("Index"); 
    } 
} 

問題 ..私は提出した後、私はアクションを作成中ため値BUT ファイルを得たフォームは常にNULLです!私は事前

答えて

1
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(MainContext model) 
{ 
    // here fetch values from model.Order and model.File 
} 

の代わりに使用すると、すべてのビューの値を得ることができることとは別に、ポストアクションに「MainContext」クラスを呼び出す二つのモデルをフェッチ中

おかげで欠場何

....

関連する問題