asp.net-mvc 视图模型 IEnumerable<> 属性从 post 方法返回 null(未绑定)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9915395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View Model IEnumerable<> property is coming back null (not binding) from post method?
提问by genxgeek
I have a view model that contains a Product class type and an IEnumerable< Product > type. On the post the first level product object comes back binded from the viewmodel whereas the product enum is coming back null.
我有一个包含 Product 类类型和 IEnumerable< Product > 类型的视图模型。在帖子中,第一级产品对象从视图模型返回绑定,而产品枚举返回空值。
Why is the IEnumerable< Prouduct> property not getting binded to my view model per the post? Thx!
为什么 IEnumerable< Prouduct> 属性没有根据帖子绑定到我的视图模型?谢谢!
Models:
楷模:
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;}
}
View:
看法:
@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>
}
}
Controller:
控制器:
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();
}
回答by Travis J
You are not using your lambda expression properly. You need to be accessing the Products list through the model. Try doing it like this:
您没有正确使用 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++
}
Edit
编辑
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)
{
// work with view model
return View();
}
Model
模型
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, Name = "Sure", Price = (decimal)(1.10) });
Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
}
public List<Product> Products { get; set; }
}
View:
看法:
@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>
}
回答by Shyju
Travis's answerwill address the issue. Here is another approach using EditorTemplates
Travis 的回答将解决这个问题。这是使用 EditorTemplates 的另一种方法
You can use an Editor templateto display the Products and it will work fine.
您可以使用Editor template来显示产品,它会正常工作。
1)Create a Folder called "EditorTemplates" under your Views/Home folder
1)在 Views/Home 文件夹下创建一个名为“EditorTemplates”的文件夹
2 )Add a view called "Products" inside that.
Add this code to that view
2)Products在里面添加一个名为“ ”的视图。将此代码添加到该视图
@model SO_MVC.Models.Product
@{
Layout = null;
}
<p>
@Html.LabelFor(x=>x.ID)
@Html.EditorFor(x=>x.ID)
</p>
<p>
@Html.LabelFor(x=>x.Name)
@Html.EditorFor(x=>x.Name)
</p>
<p>
@Html.LabelFor(x=>x.Price)
@Html.EditorFor(x=>x.Price)
</p>
@Html.HiddenFor(x=>x.Id)
and in your Main View, you can call this Editor template like this
在您的主视图中,您可以像这样调用此编辑器模板
@Html.EditorFor(m=>m.Products)

