tags:

views:

50

answers:

2

Hi,

I have the following model:

Customer:

  • ID

  • Name

  • Address

  • Phone

  • Fax

I added an Edit view based on the above model from the controller. I modified the Edit view to only allow edit on the Phone and Fax field (deleted the rest). When I submit it I get an error. It works if I leave the Edit view untouched (5 fields). However I only want to allow change in the last 2 fields.

I am lost, please help. Thanks :)

+2  A: 

If you are using the MVC ability to populate your entity/class i.e. your action sig looks like this:

ViewResult MyAction(MyObject object) {
  ...
  Save(MyObject);
}

then you'll need to make sure you include the other field, non-editable, either as visible information or using Html.Hidden within the form scope to ensure you have a fully populated object. Remember, the web is stateless and the server has no idea which record you were editing unless it has the keys to do so retrospectively.

The other option would be to retrieve the original object (for which you'll still need the primary key) from the database, update the fields from your form data and then submit the changes. We'd need to know the specific error to be able to help further, the code you are using would also be a great help.

Lazarus
Thanks Lazarus! Used Html.Hidden like suggested.
noobplusplus
instead of hidden values pass the model's id in the url like /controller/action/id. then update the model's missing properties and id in the action method before passing it to your update method/service layer/repository. Keeps your View markup cleaner
Mark
A: 

Without knowing more I would guess it has something to do with binding null to a non null property in your model. Can you give me more details on the model, the error.

If you are using the default mvc model binder then it will only bind the fields you submit. So either submit as hidden or dont use a model binder and manually map the variable from Request.Form into a copy of the model you pulled form the db.

Matthew Hood