5

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!

Community
  • 1
  • 1
Santiago
  • 2,190
  • 10
  • 30
  • 59

2 Answers2

3

Another approach is to use annotations

[HttpPost]
public virtual ActionResult Edit(
    [Bind(Prefix="", Include="field1", Exclude="field2")]MyClass myClass)
{
  ....
Steve Mallory
  • 4,245
  • 1
  • 28
  • 31
2

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

vittore
  • 17,449
  • 6
  • 44
  • 82