我有一个视图模型,它包含一个产品类类型和一个IEnumerable&产品>类型.在发布后,第一级产品对象从视图模型返回绑定,而产品枚举将返回null.
为什么IEnumerable<选取产品>属性没有被绑定到我的视图模型的每个帖子?谢谢! 楷模:
public class Product { public int ID { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class ProductIndexViewModel { public Product NewProduct { get; set; } public IEnumerable<Product> Products { get; set; } } public class BoringStoreContext { public BoringStoreContext() { Products = new List<Product>(); Products.Add(new Product() { ID = 1,Name = "Sure",Price = (decimal)(1.10) }); Products.Add(new Product() { ID = 2,Name = "Sure2",Price = (decimal)(2.10) }); } public List<Product> Products {get; set;} }
视图:
@model ProductIndexViewModel @using (@Html.BeginForm()) { <div> @Html.LabelFor(model => model.NewProduct.Name) @Html.EditorFor(model => model.NewProduct.Name) </div> <div> @Html.LabelFor(model => model.NewProduct.Price) @Html.EditorFor(model => model.NewProduct.Price) </div> <div> <input type="submit" value="Add Product" /> </div> foreach (var item in Model.Products) { <div> @Html.LabelFor(model => item.ID) @Html.EditorFor(model => item.ID) </div> <div> @Html.LabelFor(model => item.Name) @Html.EditorFor(model => item.Name) </div> <div> @Html.LabelFor(model => item.Price) @Html.EditorFor(model => item.Price) </div> } }
控制器:
public class HomeController : Controller { BoringStoreContext db = new BoringStoreContext(); public ActionResult Index() { ProductIndexViewModel viewModel = new ProductIndexViewModel { NewProduct = new Product(),Products = db.Products }; return View(viewModel); } [HttpPost] public ActionResult Index(ProductIndexViewModel viewModel) { // ???? viewModel.Products is NULL here // ???? viewModel.NewProduct comes back fine return View(); }
解决方法
你没有正确使用你的lambda表达式.您需要通过模型访问“产品”列表.尝试这样做:
@count = 0 foreach (var item in Model.Products) { <div> @Html.LabelFor(model => model.Products[count].ID) @Html.EditorFor(model => model.Products[count].ID) </div> <div> @Html.LabelFor(model => model.Products[count].Name) @Html.EditorFor(model => model.Products[count].Name) </div> <div> @Html.LabelFor(model => model.Products[count].Price) @Html.EditorFor(model => model.Products[count].Price) </div> @count++ }
编辑
控制器:
BoringStoreContext db = new BoringStoreContext(); public ActionResult Index() { ProductIndexViewModel viewModel = new ProductIndexViewModel { NewProduct = new Product(),Products = db.Products }; return View(viewModel); } [HttpPost] public ActionResult Index(ProductIndexViewModel viewModel) { // work with view model return View(); }
模型
public class Product { public int ID { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class ProductIndexViewModel { public Product NewProduct { get; set; } public List<Product> Products { get; set; } } public class BoringStoreContext { public BoringStoreContext() { Products = new List<Product>(); Products.Add(new Product() { ID = 1,Price = (decimal)(1.10) }); Products.Add(new Product() { ID = 2,Price = (decimal)(2.10) }); } public List<Product> Products { get; set; } }
视图:
@model Models.ProductIndexViewModel @using (@Html.BeginForm()) { <div> @Html.LabelFor(model => model.NewProduct.Name) @Html.EditorFor(model => model.NewProduct.Name) </div> <div> @Html.LabelFor(model => model.NewProduct.Price) @Html.EditorFor(model => model.NewProduct.Price) </div> for (int count = 0; count < Model.Products.Count; count++ ) { <div> @Html.LabelFor(model => model.Products[count].ID) @Html.EditorFor(model => model.Products[count].ID) </div> <div> @Html.LabelFor(model => model.Products[count].Name) @Html.EditorFor(model => model.Products[count].Name) </div> <div> @Html.LabelFor(model => model.Products[count].Price) @Html.EditorFor(model => model.Products[count].Price) </div> } <div> <input type="submit" value="Add Product" /> </div> }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。