tags:

views:

90

answers:

3

I have a dropdown on my homepage that I need to bind to a data source when the page loads. I have a basic controller for the page:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

I'd like to do something like this on the view page:

<select id="ddlCities">
    <% foreach (var item in ViewData.Model.Cities) { %>
        <option value='<%= item.CityID %>'><%= item.CityName %></option>
    <% } %>
</select>

So do I need to modify my Index() function to return a View Model? I'm coming from Web Forms and a little confused about how this works.

+2  A: 

You should take a look into Action Filters.

You can overwrite methods of the ActionFilterAttribute class for different states of an "action" life cycle.

OnActionExecuting – This method is called before a controller action is executed.
OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.
Henrik P. Hessel
Be careful though because `OnActionExecuted` isn't *always* called.
BuildStarted
Yeah, maybe my solution is a bit too much for his problem. But always good know about the existence of these methods.
Henrik P. Hessel
I agree with Henrik, this may be a tad complicated for what I'm trying to do. Thanks for the answer though, I didn't know about Action Filters.
Steven
+3  A: 

Steven,

You can do it even simpler than that. Just use the html helper:

<%=Html.DropDownList("ddlCities", ViewData["Cities"])%>

this 'should' work for you, as long as your model has an ienumerable list called Cities.

of course, your 'best' course of action would be to have a strongly typed model that was passed to your view, such as:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SubSonic.Web.Models.Country>" %>

(in your Model, Country would have a collection of Cities - you get the drift :))

but the above would still work.

jim
I think this is the best solution for what I'm trying to do here. Thanks.
Steven
cheers steven - glad to have stepped in for a bit..
jim
+2  A: 
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var cities = <data access code>

        return View(cities);
    }
}

@ Page Title='' Language='C#' MasterPageFile='~/Views/Shared/Site.Master' Inherits=System.Web.Mvc.ViewPage<IEnumerable<City>> 

<select id="ddlCities">
    <% foreach (var item in ViewData.Model.Cities) { %>
        <option value='<%= item.CityID %>'><%= item.CityName %></option>
    <% } %>
</select>
Rony
That would work if I only had to bind that dropdown, but I have several other dropdowns that I need to bind to data sources when the page loads. Sorry, should've been more specific.
Steven
you can either make your model more complex to hold those collections or pass the collection to the view within a ViewData dictionary like this ViewData["cities"] = ... ViewData["countries"] =... and in the view you can cast it back foreach (var item in(IEnumerable<City>) ViewData["cities"])
Rony