0
votes

I'm learning ASP.NET MVC 2, what I'm trying to do is quite simple:

  1. I populate a drop down with a list of product types
  2. When the drop down value changes I post to the model and pass through the selected value.
  3. Fill a collection with a list of products for that specific product type

Up to this point everything is working but my View is not being updated and the list of products for a specific product type is not displayed.

I was thinking maybe it's because [HTTPPost] simply posts to the view model and that it doesn't actually update the View?

Any suggestions on how I can update a view based on a drop down selection?

Model:

public class SuppliersModel { public SuppliersModel() { }

public SuppliersModel(string type)
{
    ProductsByType = Products.Where(i => i.Type == type);
}

public IEnumerable<Products> ProductsByType { get; set; }

public List<Products> Products
{
    get 
    {
        List<Products> mockProducts = new List<Products>()
        {
            new Products{Type="Fruit", Name="Apple"},
            new Products{Type="Fruit", Name="Orange"},
            new Products{Type="Vegetables", Name="Potato"},
            new Products{Type="Vegetables", Name="Carrot"}
        };
        return mockProducts;
    }
}

public SelectList ProductTypes
{
    get { return GetProductTypes(); } 
}

private SelectList GetProductTypes()
{
    var productTypes = new List<SelectListItem>
        {
            new SelectListItem{ Value="Fruit",Text="Fruit"},
            new SelectListItem{ Value="Vegetables",Text="Vegetables"}
        };

    var list = new SelectList(productTypes, "Value", "Text");
    return list;
}

}

public class Products { public string Type { get; set; } public string Name { get; set; } }

View:

<script type="text/javascript">
    $(function () {
        $('#products').change(function () {
            $.post('<%=Url.Action("GetProductsByType", "Suppliers")%>', { type: $(this).val() },
            function (result) {
            });
        });
    });
</script>
<h2>
    Suppliers</h2>
<%= Html.DropDownList("products",Model.ProductTypes) %>
<table>
    <% if (Model.ProductsByType != null) foreach (var item in Model.ProductsByType)
           { %>
    <tr>
        <td>
            <%= item.Name %>
        </td>
        <td>
            <%= item.Type %>
        </td>
    </tr>
    <% } %>
</table>

Controller:

public class SuppliersController : Controller
{
    public ActionResult Suppliers()
    {
        SuppliersModel model = new SuppliersModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult GetProductsByType(string type)
    {
        //This executes when the value in the drop down changes
        SuppliersModel model = new SuppliersModel(type);
        return View(model);
    }
}
1

1 Answers

0
votes

It looks like you are not doing anything with the html that your controller is returning after the post,

<script type="text/javascript">
    $(function () {
        $('#products').change(function () {
            $.post('<%=Url.Action("GetProductsByType", "Suppliers")%>', { type: $(this).val() },
            function (result) {
                //result will have the html that your controller returns after the post
                // you can do something like,
                $('#some-div').html(result);
            });
        });
    });
</script>