tags:

views:

47

answers:

1

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?

A: 

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.

David Andres
I already have two serparate methods for Get and Post scenarios.All the values are preserved in the post but the dropdowns. I'm using the mvc contrib extension like: <%=this.Select("CategoryId").Label("Category").Options(Model.CategoriList, "Id", "CategoryName").FirstOption("Choose").Selected(Model.CategoryId)%>
Ronnie
Well, if you need to re-render the view than you'll have to pull the set of drop-down list items. The SELECT element's value, when posted, will only contain the selected option's value. To make this easier, you could use RedirectToAction in your action method so you don't have to duplicate code.
David Andres