绑定到MVC 2中的DropDownList

时间:2010-12-13 03:31:00

标签: asp.net-mvc-2

好的,我知道我错过了一些非常简单的东西,所以也许额外的一双眼睛会有所帮助。我的DAL中有一个POCO类(State),在我的MVC应用程序中,我正在尝试将它绑定到DropDownList,但是当它加载所有我得到的是 WeddingPhotographer.Models.BusinessObjects.State 在我的DropDownList中50次。这是POCO类的代码:

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

    public virtual string StateAbbreviation
    {
        get;
        set;
    }

    public virtual string StateName
    {
        get;
        set;
    }

}

以下是我的业务对象中的代码(轻松使用术语)

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;
        }
    }
}

然后我的状态DropDownList控件

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

最后我是如何加载的

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

我知道我在这里缺少一些简单的东西(相对较新的MVC,所以仍然试图把事情搞定)

3 个答案:

答案 0 :(得分:1)

您需要使用SelectListItem的枚举来填充下拉列表中的项目。

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))%>

您可以使用Text / Value所需的任何值,但使用状态时的常见做法是使用值的缩写和文本的名称,但如果您的应用程序可以更好地使用该状态键,然后完全可以使用它。

答案 1 :(得分:1)

撤消之前执行的更改。由于您使用的是静态列表,每次使用此控件的次数是多少次?

我敢打赌,如果你在加载它之前对列表进行了空检查,并且只有在列表为空时才添加值,它将解决你的问题。

答案 2 :(得分:0)

我接受了@ SyntaxC4的建议并回到使用IEnumerable但是每当我尝试在我的View中加载DropDownList时都会得到NullReferenceException。我将DropDownList控件改为看起来像这样,它现在可以正常工作

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

感谢@ SyntaxC4和@BenAlabaster提供的电子邮件帮助