Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 2 绑定到MVC2中的DropDownList_Asp.net Mvc 2 - Fatal编程技术网

Asp.net mvc 2 绑定到MVC2中的DropDownList

Asp.net mvc 2 绑定到MVC2中的DropDownList,asp.net-mvc-2,Asp.net Mvc 2,好吧,我知道我错过了一些非常简单的东西,所以也许一双额外的眼睛会有所帮助。我的DAL中有一个POCO类(State),在我的MVC应用程序中,我试图将其绑定到DropDownList,但当它加载时,我得到的只是DropDownList中的WeddingPicture.Models.BusinessObjects.State 50次。以下是来自POCO类的代码: public partial class State { public virtual int Key {

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

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

    public virtual string StateAbbreviation
    {
        get;
        set;
    }

    public virtual string StateName
    {
        get;
        set;
    }

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

公共类状态
{
公共int密钥{get;set;}
公共字符串StateName{get;set;}
公共字符串缩写{get;set;}
}
公共类状态列表
{
私有静态IRepository状态=ObjectFactory.GetInstance();
静态只读IList stateList=new List();
公共静态可数状态
{
得到
{
foreach(weddingshopper.Data.Model.states在states.GetAll()中)
{
stateList.Add(新状态)
{
Key=s.Key,
StateName=s.StateName,
州缩写=州缩写
});
}
返回状态列表;
}
}
}
然后我的状态会下降列表控制

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

m、 国家)%>

我知道我在这里缺少了一些简单的东西(对MVC来说相对较新,所以仍在努力解决问题)

您需要有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))%>
公共类状态列表
{
私有静态IRepository状态=ObjectFactory.GetInstance();
静态只读IList stateList=new List();
公共静态可数状态
{
得到
{
foreach(weddingshopper.Data.Model.states在states.GetAll()中)
{
stateList.Add(新建SelectListItem
{
Text=s.StateName,
值=s.state缩写
});
}
返回状态列表;
}
}
}

您可以为文本/值使用任何您想要的值,但处理状态时的一种常见做法是使用值的缩写和文本的名称,但是如果您的应用程序能够更好地使用该状态键,那么使用它就完全可以了。

撤消以前所做的更改。由于您使用的是一个静态列表,您每次使用此控件的次数是多少


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

我接受了@syntax4的建议,并返回使用IEnumerable,但每当我尝试在视图中加载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在电子邮件中的帮助

谢谢,我想这就是我所缺少的,但现在我在这行收到了一个NullReferenceException我不太确定我是否理解你的意思如果列表不是空的,你的意思是什么?只向选择列表中添加不为空的项目,然后当绑定到选择列表时,其中没有任何空项目,因为在将它们添加到选择列表之前,它们都已被选中。
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WeddingPhotographer.Models.BusinessObjects.State>" %>
<%: Html.DropDownList("StatesDropDown", new SelectList(WeddingPhotographer.Helpers.StateList.States, "Key", "StateAbbreviation"))%>