问题描述
我有一个公开的方法,它返回我一个列表,称为的getStates服务层。
I have a service layer that exposes a method, which returns me a List, called GetStates.
public List<StateObject> GetStates()
我的状态对象就是我创建了一个自定义对象:
My State Object is just a custom object I have created:
public class StateObject
{
public int stateId { get; set; }
public string description { get; set; }
public Boolean isDefault { get; set; }
}
在我的模型,我试图创建将用于任务的我的显示和修改画面的典范。这将用于一件事是处理一个下拉框,这将会给支持我的任务状态列表的显示和选择。所以,我的模型看起来像这样(删除性能我们不车程约 - 这是一个大一点比这个:
In my models, I am trying to create a model that will be used for my display and modification screen of a task. One thing this will be used for is handling the display and selection of a Drop down box, which will give a list of States available for my Task. So, my model looks something like this (Removed properties we don't car about - it's a bit bigger than this:
public class TaskModifyModel
{
public int stateId { get; set; }
public string state { get; set; }
public SelectList states { get; private set; }
public TaskModifyModel()
{
states = new SelectList(
(new ReferenceService().GetStates()),
"stateId",
"description",
1);
}
}
所以,STATEID保持选中状态,状态保持选中状态的文本描述。在构造函数中,我试图创建一个国家的SelectList的观点...并填充它。
So, stateId holds the selected state, state holds the text description of the selected state. In the constructor, I am attempting to create a states SelectList for the view... and populate it.
在看,我再尝试以显示下拉列表:
In the view, I then try to display the Drop Down List:
@Html.DropDownListFor(m=>m.stateId, new SelectList(Model.states, "stateId", "description", Model.priorityId))
这是失败的,惨淡经营。
This is failing, dismally.
数据绑定:'System.Web.Mvc.SelectListItem'不包含名为'STATEID属性
我已搜查,我想我这样做的正确方法,但错误说我不是..清楚。 :)可能有人指导我,为什么它不工作,而且,这是做事的正确方法?
I have searched, and I thought I was doing this the right way, but the error says I am not.. clearly. :) Could someone guide me on why it's not working, and also, is this the right way to do things?
编辑:
下面援助之后,它现在的工作。如果我创建一个新的任务(任务id == 0),那么我得下拉的默认值,存储在我的数据库....所以,这是干净的?这是我的对象构造工作:
After assistance below, it's now working. If I am creating a new task (taskId==0), then I have to get the default value of the dropdown, as stored in my database.... So, is this clean? This is my working constructor for the object:
公共TaskModifyModel()
{
VAR referenceService =新ReferenceService();
变种p值= referenceService.GetPriorities();
变种S = referenceService.GetStates();
public TaskModifyModel() { var referenceService = new ReferenceService(); var p = referenceService.GetPriorities(); var s = referenceService.GetStates();
var defaultP = (from a in p where a.isDefault select a).FirstOrDefault();
var defaultS = (from a in s where a.isDefault select a).FirstOrDefault();
priorities = new SelectList(
(p),
"priorityId",
"description"
);
priorityId = taskId == 0 ? defaultP.priorityId : priorityId;
states = new SelectList(
s,
"stateId",
"description");
stateId = taskId == 0 ? defaultS.stateId : stateId;
}
是否确定?
推荐答案
您公开的SelectList状态{搞定;私人集; }
已经是一个的SelectList,所以你并不需要在您的视图中再次施展它。
Your public SelectList states { get; private set; }
is already a SelectList so you don't need to cast it again in your View.
试试这个:
@Html.DropDownListFor(m=>m.stateId, Model.states)
而在你的ViewModel,删除参数的SelectedValue。该@ Html.DropDownListFor将下拉初始化为正确的价值。
And in your ViewModel, remove the parameter "SelectedValue". The @Html.DropDownListFor will initialize the dropdown to the right value.
public class TaskModifyModel
{
public int stateId { get; set; }
public string state { get; set; }
public SelectList states { get; private set; }
public TaskModifyModel()
{
states = new SelectList(
(new ReferenceService().GetStates()),
"stateId",
"description");
}
}
这篇关于建设中的示范一个DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!