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,我无法获得任何价值. 解决方法生成的输入具有与模型无关的名称属性,因此不能由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脚本中的Python 500服务器错误
- asp.net – ASMX服务在开发服务器上工作,部署到IIS 7.5时返
- ASP.NET:Response.Redirect(…)后的代码会发生什么?
- asp.net – 可以通过移动设备的Web浏览器上传图片吗?
- asp.net-mvc-3 – ASP.NET MVC:如何返回304“未修改”状态
- asp.net – 如何从我的网站项目中删除未使用的CSS类?
- ASP.NET中TextBox使用Ajax控件显示日期不全的问题解决方法
- asp.net-mvc – 在发送到视图之前如何修改控制器动作中的表
- 使用什么方法将ASP.Net应用程序部署到野外?
- asp.net-mvc – MVC中的模型含义
- asp.net-mvc-4 – 在MVC4中捕获404错误
- asp.net – 如何填写一个Label.Text – 通过jQue
- asp.net-mvc – MVC 3 – Razor – 从模型打印值
- asp.net-mvc-4 – mvc4 url验证
- asp.net – ‘System.Web.UI.WebControls.TextBo
- asp.net-mvc – 如何从验证摘要中删除列表
- 嵌套的ASP.NET’应用程序’在IIS内继承父配置值?
- asp.net – SignalR不能与.Net Core一起使用
- asp.net-mvc – DevExpress MVC GridView – 如何
- asp.net-mvc-2 – asp.net-mvc2 – 不使用Model的
