C# TryUpdateModel忽略Visible=";中的属性;假;GridView的列
所以我在这里有我的更新方法:C# TryUpdateModel忽略Visible=";中的属性;假;GridView的列,c#,asp.net,gridview,webforms,model-binding,C#,Asp.net,Gridview,Webforms,Model Binding,所以我在这里有我的更新方法: public void gvProducts_UpdateItem(Int32 ProductID) { ProductListModel model = new ProductListModel(); Product product = db.Products.Find(ProductID); if (product == null) { // MAF: The item wasn't found
public void gvProducts_UpdateItem(Int32 ProductID)
{
ProductListModel model = new ProductListModel();
Product product = db.Products.Find(ProductID);
if (product == null)
{
// MAF: The item wasn't found
ModelState.AddModelError("", String.Format("Item with id {0} was not found", ProductID));
return;
}
TryUpdateModel(model);
if (ModelState.IsValid)
{
Mapper.Map(model, product);
db.SaveChanges();
gvProducts.EditIndex = -1;
}
}
它使用AutoMapper从ProductList页面的模型映射到产品实体。GridView上有很多列,因此我们根据用户选择显示和隐藏一些列。
我遇到的问题是,在TryUpdateModel()期间没有设置绑定到Visible=“false”列的任何属性,因此使用默认值填充这些属性。这意味着,例如,如果用户没有可见的价格详细信息,则在保存时,他们的价格将设置为0。因此,事实证明,该解决方案非常简单,但对于数据库来说,成本更高一些 我们从当前数据库值获取重新映射模型所需的相关实体,一旦我们从当前数据库值获取了模型,我们就可以在该模型上执行更新,这将使Visible=“false”列与其数据库值保持不变 然后,我们将模型映射回实体,并按照前面的步骤将其持久化回数据库
public void gvProducts_UpdateItem(Int32 ProductID)
{
Product product = db.Products
.Include(it => it.TaxRate)
.Include(it => it.Category)
.Include(it => it.Brand)
.Include(it => it.Supplier)
.Include(it => it.Colour)
.FirstOrDefault(it => it.ProductID == ProductID);
if (product == null)
{
ModelState.AddModelError("", String.Format("Item with id {0} was not found", ProductID));
return;
}
ProductListModel model = Mapper.Map<ProductListModel>(product);
TryUpdateModel(model);
if (ModelState.IsValid)
{
Mapper.Map(model, product);
db.SaveChanges();
gvProducts.EditIndex = -1;
}
}
public void gvProducts\u UpdateItem(Int32 ProductID)
{
产品产品=数据库产品
.Include(it=>it.TaxRate)
.Include(it=>it.Category)
.Include(it=>it.Brand)
.Include(it=>it.Supplier)
.Include(it=>it.color)
.FirstOrDefault(it=>it.ProductID==ProductID);
如果(产品==null)
{
ModelState.AddModelError(“”,String.Format(“找不到id为{0}的项”,ProductID));
返回;
}
ProductListModel模型=Mapper.Map(产品);
TryUpdateModel(model);
if(ModelState.IsValid)
{
地图(模型、产品);
db.SaveChanges();
gvProducts.EditIndex=-1;
}
}
希望这对其他人有所帮助。因此,事实证明,该解决方案实际上非常简单,但对于数据库来说成本更高 我们从当前数据库值获取重新映射模型所需的相关实体,一旦我们从当前数据库值获取了模型,我们就可以在该模型上执行更新,这将使Visible=“false”列与其数据库值保持不变 然后,我们将模型映射回实体,并按照前面的步骤将其持久化回数据库
public void gvProducts_UpdateItem(Int32 ProductID)
{
Product product = db.Products
.Include(it => it.TaxRate)
.Include(it => it.Category)
.Include(it => it.Brand)
.Include(it => it.Supplier)
.Include(it => it.Colour)
.FirstOrDefault(it => it.ProductID == ProductID);
if (product == null)
{
ModelState.AddModelError("", String.Format("Item with id {0} was not found", ProductID));
return;
}
ProductListModel model = Mapper.Map<ProductListModel>(product);
TryUpdateModel(model);
if (ModelState.IsValid)
{
Mapper.Map(model, product);
db.SaveChanges();
gvProducts.EditIndex = -1;
}
}
public void gvProducts\u UpdateItem(Int32 ProductID)
{
产品产品=数据库产品
.Include(it=>it.TaxRate)
.Include(it=>it.Category)
.Include(it=>it.Brand)
.Include(it=>it.Supplier)
.Include(it=>it.color)
.FirstOrDefault(it=>it.ProductID==ProductID);
如果(产品==null)
{
ModelState.AddModelError(“”,String.Format(“找不到id为{0}的项”,ProductID));
返回;
}
ProductListModel模型=Mapper.Map(产品);
TryUpdateModel(model);
if(ModelState.IsValid)
{
地图(模型、产品);
db.SaveChanges();
gvProducts.EditIndex=-1;
}
}
希望这对其他人有帮助