0

I have a drop down list populated with my database data when displaying user data. The problem is that when validation fails and post to the controller the drop down values are lost and I get an error. I have tried using tempdata but I get the same problem when validation fails the second time. Is there a better way to store values or maintain state without fetching everything back from the database.

Ta!

3
  • no, there isn't a clean way to store a drop down list on the view, your best option will be to requery Commented Jan 30, 2014 at 18:41
  • Use ajax to fill the dropdown on page load. Commented Jan 30, 2014 at 18:56
  • What kind of information you will be having in dropdownlist? How often the items in dropdownlist will change? Commented Jan 30, 2014 at 19:09

1 Answer 1

1

If you want to use TempData you have to do it like this:

get method
{
  ....
  TempData["yourDDLItems"] = items;
  TempData.Keep();
}

post method
{
  TempData.Keep();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works great. I basically wanted temp storage for the data. Another approuch is to store dropdown data in a hidden field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.