2016-08-21 63 views
2

形式是這樣的:https://gyazo.com/289a1ac6b7ecd212fe79eec7c0634574帖子列表

視圖模型:

public class ProductViewModel 
{ 
    public string Product { get; set; } 
    public IEnumerable<SizeColorQuantityViewModel> SizeColorQuantities { get; set; } 
} 
public class SizeColorQuantityViewModel 
{ 
    public string ColorId { get; set; } 
    public List<SizeAndQuantity> SizeAndQuantities { get; set; } 
} 
public class SizeAndQuantity 
{ 
    public int SizeId { get; set; } 
    public int Quantity { get; set; } 
} 

查看:

@model ProjectSem3.Areas.Admin.Models.ProductViewModel 
@{ 
    ViewBag.Title = "Create"; 
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml"; 
    string[] ListColor = { "Red", "Blue" }; 
    string[] ListSize = { "S", "M", "L", "XL" }; 
} 
    @for (var i = 0; i < ListColor.Length; i++) 
    { 
    <div class="form-group"> 
     <label class="col-md-2 control-label">Color:</label> 
     <div class="col-md-2"> 
      @Html.TextBox("[" + i + "].ColorId", null, new { @Value = ListColor[i], @class = "form-control", @readonly = "readonly" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <label class="col-md-2 control-label">Size and Quantity:</label> 
     @for (var j = 0; j < ListSize.Length; j++) 
     { 
     <div class="col-md-2"> 
      @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.SizeId", null, new 
      { 
      @class = "form-control", 
      @style = "margin-bottom: 15px", 
      @Value = ListSize[j], 
      @readonly = "readonly" 
      }) 
      @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.Quantity", null, new { @class = "form-control" }) 
     </div> 
     } 
    </div> 
    } 

控制器:

// GET: Admin/Product 
public ActionResult Create() 
{ 
    return View(); 
} 
// POST: Admin/Product 
[HttpPost] 
public ActionResult Create(ProductViewModel product, IEnumerable 
<SizeColorQuantityViewModel> 
sizeColorQuantity, IEnumerable 
<SizeAndQuantity> 
sizeAndQuantity) 
{ 
    return View(); 
} 

,我可以得到它通過從視圖模型IEnumerable<SizeColorQuantityViewModel> sizeColorQuantity到控制器值。但是用這個型號IEnumerable<SizeAndQuantity> sizeAndQuantity,我無法獲得任何價值。 因爲這是二維數組,所以我不知道這個問題。您能否教我如何將值綁定爲IEnumerable<SizeAndQuantity> sizeAndQuantity.

+1

與名稱屬性您生成輸入不涉及到你的模型,但不是很多這段代碼正在感。爲什麼你的視圖中有'ListColor'和'ListSize'集合?您是否在嘗試編輯紅/小,紅/中,紅/大等產品的數量,並再次針對藍/小,藍/中等等進行編輯? –

+0

@StephenMuecke是的,我也想過很多方式來發布大小,顏色和數量的產品。你有沒有其他的方法?如果你不介意,你能更多地解釋我嗎?非常感謝:D –

+1

您需要進行很多更改才能完成此項工作。你可以改變IEnumerable 到'List 嗎?(它會讓它更容易) –

回答

3

您生成的輸入具有與您的模型無關的名稱屬性,因此不能受DefaultModelBinder的約束。您需要先在控制器中生成數據(而不是在視圖中),以便綁定到模型。

下面假設你SizeColorQuantities屬性更改爲List<SizeColorQuantityViewModel>

public ActionResult Create() 
{ 
    var colors = new List<string>(){ "Red", "Blue" }; 
    var sizes = new List<string>(){ "S", "M", "L", "XL" }; 

    var model = new ProductViewModel() 
    { 
     Product = "My product", 
     SizeColorQuantities = new List<SizeColorQuantityViewModel> 
    }; 
    foreach(var color in colors) 
    { 
     var child = new SizeColorQuantityViewModel() 
     { 
      ColorId = color, 
      SizeAndQuantities = new List<SizeAndQuantity> 
     }; 
     model.SizeColorQuantities.Add(child); 
     foreach(var size in sizes) 
     { 
      child.SizeAndQuantities.Add(new SizeAndQuantity() 
      { 
       SizeId = size // assumes SizeId is changed to string, not int 
      }); 
     } 
    } 
    return View(model); 
} 

你現在有將被傳遞給視圖一個正確填充視圖模型,它可以綁定到內部嵌套for與強類型HtmlHelpers環路

@model ProductViewModel 
.... 
@using (Html.BeginForm()) 
{ 
    .... 
    @for(int i = 0; i < Model.SizeColorQuantities.Count; i++) 
    { 
     @Html.TextBoxFor(m => m.SizeColorQuantities[i].ColorId, new { @class = "form-control", @readonly = "readonly" }) 
     for (int j = 0; j < Model.SizeColorQuantities[i].SizeAndQuantities .Count; j++) 
     { 
      @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].SizeId, new { @class = "form-control", @readonly = "readonly" }) 
      @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].Quantity, new { @class = "form-control" }) 
     } 
    } 
    <input type="submit" ... /> 
} 

注:始終使用ST rongly鍵入***For()HtmlHelper方法和從不嘗試使用HtmlHelper方法時設置value(或name)屬性。

你的POST方法現在需要

[HttpPost] 
public ActionResult Create(ProductViewModel model) 
{ 
    .... 
} 

注意,您還可以使用自定義EditorTemplate的爲你的類型爲HTML Table to ADO.NET DataTable,這也解釋瞭如何您name屬性必須以結合生成解釋集合。在你的情況,例如您的Quantity輸入必須(比較,爲你的當前生成)

<input name="SizeColorQuantities[0].SizeAndQuantities[0].Quantity" ... /> 
<input name="SizeColorQuantities[0].SizeAndQuantities[1].Quantity" ... /> 
.... 
<input name="SizeColorQuantities[1].SizeAndQuantities[0].Quantity" ... /> 
<input name="SizeColorQuantities[1].SizeAndQuantities[1].Quantity" ... /> 
.... 
+0

你救了我的命。我從這個例子中學到了很多東西。非常感謝(弓) –

+0

我可以再問你一個問題嗎?我釋放從HttpPost返回的模型爲null。你有什麼想法嗎? (ProductViewModel model) var test = product.Product; return View(); } –

+0

對不起@Stephen Muecke。我發現如何解決這個問題。原因我設置參數變量是「產品」,它是與屬性名稱相同的名稱。 非常感謝你。 http://stackoverflow.com/a/35889181/4642316 –