tags:

views:

141

answers:

1

I have an edit view that is displaying some model properties as plain text and other model properties as input fields. My problem is that the model properties displayed as plain text are null when the model is returned to the view after a validation error.

How do I retain the model values when I don't have input fields for all properties? What is best practice?

My first thought is to keep the values of these properties in hidden input elements.

<%= Html.HiddenFor(model => model.CustomerName) %>

Is this considered a correct solution?

Thanks Andreas

+3  A: 

You can either:

  1. Use hidden fields, as you suggest, or
  2. Re-populate the "missing" fields before returning the view in the POST action.

Pick whichever works for your app.

Craig Stuntz
I would add that the mentality when coming from a web forms background is to try and keep the fields, but if you sit down and really think about it, the overhead for repopulating is not a deal breaker for most applications, and makes maintenance a lot easier.
Paddy
Paddy, you are right. Repopulating is a cleaner solution and in my case performance is not an issue. I will go for that. Thanks!
Andreas
Another option would be passing back the same model that was posted with/by the form, that way you'll keep the values just as the user typed them.
JoseMarmolejos