views:

146

answers:

1

I am trying to understand how best to organize some common Dropdown lists used in several views (some are cascading)

Is it best to create a single \Models\CommonQueries then create a webservice for each dropdown used in cascading situation then have a single controller that contains actions for each dropdowns

This way I can follow DRY principle and not repeat the dropdown logics since they are used in various views.

Much Thanks and Regards for reading my question and taking the your time. +ab

+2  A: 

When you say your dropdowns are used in several views, do you still consider these dropdowns as part of the view that is rendering them? If so, I think using a custom HTML helper or a partial view (ascx) is appropriate. Then, like you suggest, you can populate the data for the dropdowns using a common service from your domain layer. I think that is a very reasonable approach.

However, if you feel the dropdowns are somewhat external/unrelated to the view, then you might find that using Html.RenderAction() gives you a much cleaner result. Using Html.RenderAction(), you can output the result of an Action method directly into any other view. Therefore, you can create 1 controller with the necessary Action method(s) to populate those dropdowns. For example, let say you have a view with roughly something like:

<div>
    <div id="coreView1">
        <!-- some view code here -->
    </div>
</div>

<div id="commonDropdowns">
        <% Html.RenderAction("Create", "Dropdown"); %>
</div>

where Create is the name of your method in the DropdownController.

For example:

public class DropdownController : Controller
{
    public ViewResult Create()
    {
        // do stuff here to create the ViewResult of the common Dropdowns
    }
}

Note: Some people dislike this approach as it doesn't fit the typical MVC seperation of concerns. However, it can be a really great fit for some cases.

Hope one of these approaches can help.

Chris Melinn
It should be noted that Html.RenderAction is NOT included with ASP.NET MVC 1.0. It is a seperate Add-On. http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx But, it will be included with ASP.NET MVC 2.0 Beta (finally!).
eduncan911
Thanks eduncan, good point! Some info and references here: http://davidhayden.com/blog/dave/archive/2009/04/04/ASPNETMVCPartialViewsHtmlRenderActionASPNETMVCFutures.aspx
Chris Melinn
Thank you for your replies. I am in MVC1. I may have to port it to MVC2 and try out. But despite that in MVC1, I have gone the route of organizing \Model\CommonQueries and use that in those in the views where the dropdown is used and the view where it is maintained. Trying to get cascading to work with webservice now but that is not working out. Going the route of JSON now. It's fun fun fun. Thanks again.
ab
You can also use in MVC1 as long as you include MVC futures. The link is included in my comment above. Also, please upvote and/or mark as correct answer if this has been helpful. Thanks!
Chris Melinn