Ignore optional columns on save

自古美人都是妖i 提交于 2019-12-23 15:09:25

问题


When I update a table from a Form through Autogenerated EF, if I remove some data-columns from the view form because I don't want to be editable, that columns are updated with null value, how can avoid this behavior? I read here: Entity Framework: Ignore Columns removing it from the model, but not always I want to ignore these datacolumns.

thank!


回答1:


asp.net MVC provide you with UpdateModel method, look on the overload

protected internal void UpdateModel<TModel>(
TModel model,
string prefix,
string[] includeProperties,
string[] excludeProperties
)
where TModel : class

using it you are able to exclude or include particular properties by their names




回答2:


Another approach is to use annotations

[HttpPost]
public virtual ActionResult Edit(
    [Bind(Prefix="", Include="field1", Exclude="field2")]MyClass myClass)
{
  ....


来源:https://stackoverflow.com/questions/6076167/ignore-optional-columns-on-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!