asp.net-mvc-5 – ASP.NET MVC中Controller的模型对象列表的列表

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-5 – ASP.NET MVC中Controller的模型对象列表的列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
形式如下: 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",new
          {
          @class = "form-control",@style = "margin-bottom: 15px",@Value = ListSize[j],@readonly = "readonly"
          })
          @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.Quantity",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();
}

我可以获得从viewmodel IEnumerable< SizeColorQuantityviewmodel>传递的值. sizeColorQuantity to Controller.但是使用这个模型IEnumerable< SizeAndQuantity> sizeAndQuantity,我无法获得任何价值.
因为这是2-D阵列所以,我不知道这个问题.你能教我如何绑定IEnumerable< SizeAndQuantity>的值吗? sizeAndQuantity.

解决方法

生成的输入具有与模型无关的名称属性,因此不能由DefaultModelBinder绑定.您需要首先在控制器中生成数据(而不是在视图中),以便绑定到模型.

以下假设您将SizeColorQuantities属性更改为List< SizeColorQuantityviewmodel>

public ActionResult Create()
{
    var colors = new List<string>(){ "Red","Blue" };
    var sizes = new List<string>(){ "S","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循环中的强类型HtmlHelper将其绑定到该视图

@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,@readonly = "readonly" })
            @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].Quantity,new { @class = "form-control" })
        }
    }
    <input type="submit" ... />
}

注意:始终使用强类型*** For()HtmlHelper方法,并且在使用HtmlHelper方法时永远不要尝试设置值(或名称)属性.

你的POST方法现在需要

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

请注意,您还可以使用自定义EditorTemplate作为类型,如HTML Table to ADO.NET DataTable中所述,这也解释了如何生成名称属性以绑定到集合.在您的情况下,例如您的数量输入需要(与您当前生成数量相比)

<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" ... />
....

猜你在找的asp.Net相关文章