Asp – way to preserve Dropdown list valuses

asp.net-mvc

I have the classic scenario when a form is rendered from an action method returning a view.

The view contains some dropdown lists that are prefilled (like calling some repository methods) before the view is rendered and a DTO is passed to the view.

When the form is posted I would like to re-render the same view without getting again the values for the dropdowns similarly to how the textboxes and other form controls preserve their state.

Apparently when posting the form only the selected value in the dropdowns is posted.

What is the best way to do that?

Best Answer

Try having two separate actions: one with the attribute AcceptVerbs.Get set, which will be used to initially render the view. The other will have the attribute AcceptVerbs.Post, and this action will fire when a POST event occurs.

You can simply return View() from the Post version when done. The ASP.NET MVC engine is smart enough to not force you to re-render the entire view.

Related Topic