4

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.

0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the best solution for what I'm trying to do here. Thanks.
3

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.

3 Comments

Be careful though because OnActionExecuted isn't always called.
Yeah, maybe my solution is a bit too much for his problem. But always good know about the existence of these methods.
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.
2
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>

2 Comments

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.
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"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.