开发者

Bind to DropDownList in MVC 2

Ok I know I'm missing something real simple here so maybe an extra set of eyes will help. I have a POCO class (State) in my DAL, and in my MVC application I'm trying to bind that to a DropDownList, but when it loads all I get is WeddingPhotographer.Models.BusinessObjects.State in my DropDownList 50 times. Here's the code from the POCO class:

public partial class State
{
    public virtual int Key
    {
        get;
        set;
    }

    public virtual string StateAbbreviation
    {
        get;
        set;
    }

    public virtual string StateName
    {
        get;
        set;
    }

}

Here's the code from my business Object (use term lightly)

public class State
{
    public int Key { get; set; }
    public string StateName { get; set; }
    public string StateAbbreviation { get; set; }
}

public class StateList
{
    private static IRepository<WeddingPhotographer.Data.Model.State> states = ObjectFactory.GetInstance<IRepository<WeddingPhotographer.Data.Model.State>>();
    static readonly IList<State> stateList = new List<State>();

    public static IEnumerable<State> States
    {
        get
        {
            foreach (WeddingPhotographer.Data.Model.State s in states.GetAll())
            {
                stateList.Add(new State
                {
                    Key = s.Key,
                    StateName = s.StateName,
                    StateAbbreviation = s.StateAbbreviation
                });
            }
            return stateList;
        }
    }
}

Then my states DropDownList control

<%@ 开发者_运维问答Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WeddingPhotographer.Models.BusinessObjects.State>" %>
<%: Html.DropDownList("StatesDropDown", new SelectList(WeddingPhotographer.Helpers.StateList.States,Model))%>

And finally how I'm loading it

<div class="editor-field">
    <% Html.RenderPartial("StatesDropDown"); %>
    <%= Html.ValidationMessageFor(m => m.State) %>
</div>

I know I'm missing something simple here (relatively new to MVC so still trying to get things right)


Undo the Change that you did previously. Due to the fact that you're using a static list, how many times are you using this control per chance?

I bet if you did a null check on the list before loading it and only adding values if the list is null it would fix your problem.


you need to have an enumeration of SelectListItem to populate the items from the dropdown.

public class StateList
{
    private static IRepository<WeddingPhotographer.Data.Model.State> states = ObjectFactory.GetInstance<IRepository<WeddingPhotographer.Data.Model.State>>();
    static readonly IList<SelectListItem> stateList = new List<SelectListItem>();

    public static IEnumerable<SelectListItem> States
    {
        get
        {
            foreach (WeddingPhotographer.Data.Model.State s in states.GetAll())
            {
                stateList.Add(new SelectListItem
                {
                    Text = s.StateName,
                    Value = s.StateAbbreviation
                });
            }
            return stateList;
        }
    }
}

<%: Html.DropDownList("StatesDropDown", new SelectList(WeddingPhotographer.Helpers.StateList.States,Model.StateAbbreviation))%>

You can use any values you want for the Text/Value but a common practice when working with states is to use the abbreviation for the Value and the Name for the text, but if your application would work better with that state Key, then it's perfectly ok to use it.


I took @SyntaxC4's advice and went back to using IEnumerable but was getting a NullReferenceException whenever I would try to load the DropDownList in my View. I changed my DropDownList control to look like this and it now works perfectly

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WeddingPhotographer.Models.BusinessObjects.State>" %>
<%: Html.DropDownList("StatesDropDown", new SelectList(WeddingPhotographer.Helpers.StateList.States, "Key", "StateAbbreviation"))%>

Thanks @SyntaxC4 and @BenAlabaster for your help in email as well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜