Assume I have Entity Framework setup correctly, and the POCO classes are have the proper relationships, etc.
<!-- language: c# -->
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string SomeFieldIDontWantUsersToEdit { get; set; } // there are a lot of these
public IList<Address> Addresses { get; set; }
}
public class Address
{
public string EntireAdressInOneLine { get; set; }
}
When I display things to the user, I only want to show FirstName and LastName, and the addresses. That works and the view is functioning properly.
This is my view:
<!-- language: c# -->
@using (Html.BeginForm(actionName: null, controllerName: null, method: FormMethod.Post))
{
@Html.TextBoxFor(model => Model.Person.FirstName)
@Html.TextBoxFor(model => Model.Person.LastName)
@Html.HiddenFor(model => Model.Person.Id)
}
But when it comes time to Update this entry, I'm lost. If this is the action for update:
<!-- language: c# -->
[HttpPost]
public ActionResult UpdatePerson(int personId, Person updatedPerson)
{
if (ModelState.IsValid)
{
}
}
When I submit the form, the model binder will pickup that I want a Person object. Cool, that works. Of course, SomeFieldIDontWantUsersToEdit will be null since it wasn't included in the view -- I understand that part.
My question is, is there some way to tell EF that I only want to update certain properties in this entity, and use whatever value was there already for all other properties.
I want the original values of SomeFieldIDontWantUsersToEdit to say the same, but want to allow the user to edit First and Last name. If it were only 1 or 2 fields, sure, I can do it manually, but it's a lot more fields in the actual code.
Maybe a better question is, what is the proper/recommended way of doing this?