我有一个视图模型,它包含一个产品类类型和一个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> }