views:

36

answers:

1

Hi, My problem is that I want to partially edit my model - to display 2 fields and to edit another 2 fields for example. When POST happens, returned model contains only the fields that are editable, other fields that I use only to display are NULL. How to fix this, on POST to be returned model with all fields, because on ERROR when I return this model and fields are NULL is not so good?

+2  A: 

The model binder binds the form values only to model properties that have a setter.

Depending on what you need to achieve:

You can use hidden inputs to store the values in the view, these will be bound back (given that the properties have a setter)

<%= Html.Hidden(Model.SomeField) %>

class YourViewModel
{
    public SomeField {get; set;}

Or you should ensure in your controller action that you are only updating the fields that you displayed in the view, not the null ones.

Ideally, your viewmodel should contain only the properties that are relevant for the view (and for the logic handled by the controller).

Marek