ASP.NET MVC Page - Viewstate for Confirm email field is getting erased on Registration Page if validation fails?

It's because it is not using your Model All your other fields are using TextBoxFor and that one is not so it isn't getting auto wired up to post back and forth pre-filling your model If you want it to have the value, your need to manually put it back in to the control or add it to your Model and it will automatically fill on your controller and when you pass the model back to your view it will build with the value that was pre-existing You can also add it's value to the ViewData the same name as the control's ID Either way you need to fetch the value in your controller and then pass it back to the view so the error message can be displayed. If you add it to your Model then it will be done automatically for you This is why it's nice to have a seperate view model that wraps all the data in your view and not just what you want to save to your DB. Your view shouldn't reflect only what your DB wants.

It's because it is not using your Model. All your other fields are using TextBoxFor and that one is not so it isn't getting auto wired up to post back and forth pre-filling your model. If you want it to have the value, your need to manually put it back in to the control or add it to your Model and it will automatically fill on your controller and when you pass the model back to your view it will build with the value that was pre-existing.

You can also add it's value to the ViewData the same name as the control's ID. Either way you need to fetch the value in your controller and then pass it back to the view so the error message can be displayed. If you add it to your Model then it will be done automatically for you.

This is why it's nice to have a seperate view model that wraps all the data in your view and not just what you want to save to your DB. Your view shouldn't reflect only what your DB wants...

Since the property doesn't exist on your model, you can add a value to be persisted into the ModelState dictionary and it will automatically show up when you redisplay the form. Something like: ModelState. Add("confirm_email", Request.

Form"confirm_email").

I m trying to Add the Key to the ModelState as you recommended. But I am running into errors. – Rita Apr 26 '10 at 23:21.

I have a Registaration page with the following fields Email, Confirm Email, Password and Confrim Password. On Register Button click and post the model to the server, the Model validates and if that Email is already Registered, it displays the Validation Error Message "User already Exists. Please Login or Register with a different email ID".

While we are displaying this validation error message, I am loosing the value of "Confirm Email" field. So that the user has to reenter again and I want to avoid this. Here I don't have confirm_Email field in my Model.

Is there something special that has to be done to remain Confirm Email value on the Page even in case of Validation failure? Appreciate your responses.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions