MVC DropDownListfor()基础 [英] MVC DropDownListfor() Basics
问题描述
我有一个包含员工信息的模型。在我的模型中,有一个名为City的属性,它定义了他所居住的Employee所在的城市。属性显示如下
I have a Model which consist of Employees information. In my model there is a property called City which define the city of Employee in which he lives. The propery is shown below
public string City{get;set;}
现在,我有一个视图,其中包含一个表格,该表格将由员工填写以进行注册。我想使用下拉列表选择城市。我认为以下代码将用于下拉菜单,正如我发现的那样。我的模型名称是Employee。
Now I have a view which contains a form which will be filled by a employee to register. I want to use a dropdownlist for selecting cities. I think the below code will be used for dropdown as i discovered. My model name is Employee.
@Html.DropDownListFor(m=>m.City,new SelectList())
请告诉我,可以直接在SelectList()方法中为dropdownlist定义选项吗? .. in html?
Please tell me that "is there any way to define the options for dropdownlist in SelectList() method directly Like ... in html?"
如果没有,我应该在哪里定义该下拉列表的类,在哪里调用以及在哪里渲染。我不知道在哪里定义值?我很困惑,因为这是mvc,我们必须分开关注,我认为我们无法在任何地方定义任何内容?
If not, where should i define the class for this drop down, where to call and where to render.I don't know where to define values? I am very confused because this is mvc and we have to seperate concern and i think we cannot define anything at anywhere?
在此先感谢。.
推荐答案
您至少有两个选择:
1。)添加列表,数组,或模型中其他任何类型的城市
1.) Add a list, array, or any other collection type of cities to your model
2。)将SelectList属性添加到模型中
2.) Add a SelectList property to your model
选项1可以是简单的字符串数组,也可以是 City
对象的 IEnumerable
。然后,您需要将该属性转换为视图中 SelectListItem
对象的集合,作为 DropDownList
绑定的一部分。
Option 1 can be something as simple as an array of strings, or can be, say, an IEnumerable
of City
objects. You would then need to transform this property to a collection of SelectListItem
objects in the view as part of the DropDownList
binding.
选项2的优点是能够直接绑定到 DropDownList
,但要求您构造列表
Option 2 has the advantage of being capable of direct binding to the DropDownList
, but requires that you construct the list within the action method.
然后,最终结果是相同的,这只是您想成为SoC的脚问题。
Then end result is the same, it's just a matter of how pedantic you want to be about SoC.
例如(假设您添加了一个名为 Cities
的属性):
For example (assuming you add a property called Cities
):
@Html.DropDownListFor(m=>m.City, Model.Cities.Select(city => new SelectListItem()
{
Text = city,
Value = city,
Selected = city == Model.City
})
编辑:
要回答您的评论,我必须做一些假设,假设您有一个名为的模型EmployeeModel
。此模型具有一个属性,城市
,t帽子是普通的绳子。因此,这是您模型的一部分,因为我认为它是:
To answer your comment, I have to make some assumptions. I will assume you have a model called EmployeeModel
. This model has a property, City
, that is a plain string. So, this is a partial of your model, as I assume it to be:
public class EmployeeModel
{
public string City { get; set; }
// ... other properties ...
}
因此,如果您需要添加属性以绑定到下拉菜单,则可以执行以下操作之一:
So, if you need to add a property for binding to your dropdown, you would do one of the following:
public class EmployeeModel
{
public string City { get; set; }
public IEnumerable<string> Cities { get; set; }
// ... other properties ...
}
或
public class EmployeeModel
{
public string City { get; set; }
public SelectList Cities { get; set; }
// ... other properties ...
}
此新属性将包含您允许用户选择的城市列表。
This new property will contain the list of cities that you allow your user(s) to pick from.
如果选择第一个选项,则从数据存储区加载IEnumerable,然后在视图中使用上面的第一个示例,该示例使用LINQ来投影将城市
属性添加到新的 SelectListItem
对象中。
If you choose the first option, you load the IEnumerable from your datastore, and then use the first example above in your view, which uses LINQ to project each string in the Cities
property into a new SelectListItem
object.
如果使用第二个选项,则在将模型传递给操作之前,在操作中构建 SelectList
视图。这并不是很困难,因为该类提供了一个构造函数,该构造函数采用 IEnumerable
(您的城市列表)和选定值,即城市
属性(请参见 http://msdn.microsoft.com/en-us/library/dd460123%28v=vs.108%29.aspx )。您的代码如下所示:
If you go with the second option, you build a SelectList
in the action prior to passing the model to the view. This isn't terribly difficult, as the class provides a constructor that takes an IEnumerable
(your list of cities) and the "selected value," which will be the City
property (see http://msdn.microsoft.com/en-us/library/dd460123%28v=vs.108%29.aspx). Your code would look something like:
model.Cities = new SelectList(GetCities(), model.City);
当然,这假定您具有帮助方法( GetCities()
)从存储城市的任何地方加载城市。这样,您的视图将具有以下内容:
This, of course, assumes you have a helper method (GetCities()
) to load your cities from wherever they are stored. Your view then would have something like this:
@Html.DropDownListFor(m=>m.City, model.Cities)
然后,视图引擎使用这些 SelectListItem
s构建< select>
元素,而它是< option>
元素。
The view engine then uses these SelectListItem
s to build the <select>
element and it's <option>
elements.
这篇关于MVC DropDownListfor()基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!