绑定到 MVC 2 中的 DropDownList
好吧,我知道我在这里错过了一些非常简单的东西,所以也许额外的一双眼睛会有所帮助。我的 DAL 中有一个 POCO 类(状态),在我的 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 相对较新,所以仍在努力把事情做好)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
撤消您之前所做的更改。由于您使用的是静态列表,您平均使用此控件多少次?
我敢打赌,如果您在加载列表之前对列表进行了空检查,并且仅在列表为空时才添加值,那么它会解决您的问题。
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.
您需要有一个 SelectListItem 的枚举来填充下拉列表中的项目。
您可以使用任何您想要的文本/值值,但处理状态时的常见做法是使用值的缩写和文本的名称,但如果您的应用程序可以更好地使用该状态键,那么它就完美了可以使用它了。
you need to have an enumeration of SelectListItem to populate the items from the dropdown.
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.
我采纳了 @SyntaxC4 的建议并重新开始使用 IEnumerable,但每当我尝试在视图中加载 DropDownList 时,都会收到 NullReferenceException。我将 DropDownList 控件更改为如下所示,现在它可以完美运行,
感谢 @SyntaxC4 和 @BenAlabaster 在电子邮件中提供的帮助
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
Thanks @SyntaxC4 and @BenAlabaster for your help in email as well