2017-03-15 87 views
0

我有一個ProductViewModel,其中每個產品都有多個圖像Bullet_Points.I能夠顯示數據以編輯函數但是在將數據捕獲回POST編輯方法。無法將集合項從視圖綁定到控制器:

@model OMS.ViewModels.ProductVm2 
@using(Html.BeginForm()) { 
    @Html.AntiForgeryToken() <h4> ProductVM < /h4> < hr/> 
    < div class = "form-horizontal" > 


    @Html.HiddenFor(m => m.Product.ProductID) 


     @foreach (var img in Model.Images) 
    { 
    <div class="form-group"> 
     @Html.LabelFor(modelItem => img.Image_URL, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(modelItem => img.Image_URL, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(@modelItem => img.Image_URL, "", new { @class = "text-danger" }) 
     </div> 
    </div> 
    } 

我應該如何捕捉圖像集合到控制器[HTTPPOST]編輯方法。因爲該方法應迭代圖像模型。我能夠在沒有問題的情況下捕捉其他模型數據。

public ActionResult Edit(FormCollection form, ProductVm2 VM) { 

    Product product = new Product() { //Supplier_ID = VM.Product.Supplier_ID, 
    Name = VM.Product.Name, Description = VM.Product.Description, Product_Code = VM.Product.Product_Code, 
     ProductID = VM.Product.ProductID 
    }; 

//Images 
List<Image> Images = new List<Image>(); 

Images.Add(......) 

}

而且當我試圖檢查從查看被送到Controller.I的對象看到的圖像對象爲空,即使我已經通過了數據進去。

enter image description here

回答

0

你真的不能使用foreach和有此工作,默認的模型綁定希望集合索引語法正確創建綁定,能元素ID .....

,而不是

@foreach (var img in Model.Images) 
{ 
<div class="form-group"> 
    @Html.LabelFor(modelItem => img.Image_URL, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(modelItem => img.Image_URL, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(@modelItem => img.Image_URL, "", new { @class = "text-danger" }) 
    </div> 
</div> 
} 

使用此

@for (int i = 0; i < Model.Images.Count(); i++) 
{ 
<div class="form-group"> 
    @Html.LabelFor(x=> x.Images[i].Image_URL, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(x=> x.Images[i].Image_URL, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(x => x.Image[i].Image_URL, "", new { @class = "text-danger" }) 
    </div> 
</div> 
} 
相關問題