views:

552

answers:

2

Hi all,

I have a view that looks somewhat similar to this

 <% using (Html.BeginForm()) {%>
    <%= Html.DropDownList("Category") %>
    <%= Html.DropDownList("SubCategory") %>
    <input type="submit" value="Print" />
    <%= Html.ActionLink("Cancel", "Index") %>
<% } %>

I was wondering if anyone knew how i could load the subcategory based on the selected Category? In webforms i'd just use the autopostback event to do this, but i'm a bit confused on how to do this using the mvc framework.

Thanks in advance

+1  A: 

Place the dropdown list within a PartialView. Then when you post back do a return PartialView("viewName", model). Then in the return of your jQuery simply replace the partial view with the new html that is returned.

So you're view;

<div id="myPartialView">
  <% Html.PartialView("PartialViewName", model); %>
</div>

Then your jQuery does something like

$('#myPartialView').html = retHtml;

Your C#

return PartialView("PartialViewName", model);

Untested but that's the approach i think you want to take.

griegs
+2  A: 

transform your view like this:

 <% using (Html.BeginForm()) {%>
    <%= Html.DropDownList("Category", Model.SelectList, new {onchange = "actualize(this);"}) %>
    <div id="selectdiv">
    <% Html.RenderPartial("SubCategories"); %>
    </div>
    <input type="submit" value="Print" />
    <%= Html.ActionLink("Cancel", "Index") %>
<% } %>

<script type="text/javascript">

function actualize(obj)
{
     $.ajax({
        url: url,
        async: true,
        type: 'POST',
        data: { id: obj.value },
        dataType: 'text',
        success: function(data) { $("#selectdiv").html(data); },
        error: function() {
            console.log('Erreur');
        }
    });
}

</script>

create a control called SubCategories.aspx and include in it:

<%= Html.DropDownList("SubCategory",Model.SelectList) %>

create a Model class

public class MyModel
{
    public SelectList SelectList {get;set;}
}

create a controller action

public ActionResult SubCategories(int id)
{
     MyModel model = new MyModel();
     model.SelectList = new SelectList(YourRepository.GetSubCategories(id),"Id","Name");
     return View(model);
}
Gregoire
Prefer to see the dropdown in a partial view so that it can be re-used. so instead of having the two dropdowns you could have a single partial view. makes styling a little easier and you only need to do things once. i know this is a little redundant with a single dropdown but in my experiance this then becomes second nature with bigger reuasable componants
griegs
If you read, the second drop down is in a partial view called SubCategories.aspx :)
Gregoire
If there was another drop down after sub categories which depended both on categories and subcategories would you recommend placing that in another partial view as well?
AlteredConcept
Yes it is what I usually do
Gregoire
Nice code. You can see here another way to do the same http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx and http://www.michaeljbaird.com/post/2009/04/13/ASPnet-MVC-and-JQuery-Cascading-Droplist.aspx
Junior Mayhé