【发布时间】:2010-08-03 15:00:33
【问题描述】:
我有以下型号:
public class Product {
public int Id { get; set; }
public string Name { get; set; }
private int CategoryId { get; set; }
public Category Category { get; set; }
public string InventoryDetails { get; set; }
}
我的控制器中有一个用于创建新产品的操作。我的问题是如何限制可以从 POST 数据绑定的模型的属性?因为我只希望用户 POST 数据绑定 Name 和 CategoryId。还是创建一个只有这些属性可以绑定的单独视图模型更好?
public ActionResult Create(Product p)
或
public ActionResult Create(CreateProductViewModel model)
在哪里
public class CreateProductViewModel {
public string Name {get; set;}
public int CategoryId {get;set;}
}
【问题讨论】:
标签: asp.net-mvc entity-framework validation