gen*_*eek 11 asp.net-mvc asp.net-mvc-3
我有一个包含Product类类型和IEnumerable <Product>类型的视图模型.在帖子上,第一级产品对象从视图模型返回绑定,而产品枚举返回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;}
}
Run Code Online (Sandbox Code Playgroud)
视图:
@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>
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
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();
}
Run Code Online (Sandbox Code Playgroud)
Tra*_*s J 14
您没有正确使用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++
}
Run Code Online (Sandbox Code Playgroud)
编辑
控制器:
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();
}
Run Code Online (Sandbox Code Playgroud)
模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
@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>
}
Run Code Online (Sandbox Code Playgroud)
特拉维斯的回答将解决这个问题.这是使用EditorTemplates的另一种方法
您可以使用an Editor template来显示产品,它将正常工作.
1)在Views/Home文件夹下创建一个名为"EditorTemplates"的文件夹
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)
Run Code Online (Sandbox Code Playgroud)
在主视图中,您可以像这样调用此编辑器模板
@Html.EditorFor(m=>m.Products)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29705 次 |
| 最近记录: |