C# MVC DropDownListfor() 基础
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15195256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC DropDownListfor() Basics
提问by Sometimes Code
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
我有一个包含员工信息的模型。在我的模型中,有一个名为 City 的属性,它定义了 Employee 居住的城市。属性如下图
public string City{get;set;}
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())
Please tell me that "is there any way to define the options for dropdownlist in SelectList() method directly Like ... in html?"
请告诉我“有什么方法可以直接在 SelectList() 方法中定义下拉列表的选项,就像...在 html 中一样?”
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?
如果没有,我应该在哪里定义这个下拉列表的类,在哪里调用以及在哪里渲染。我不知道在哪里定义值?我很困惑,因为这是 mvc,我们必须单独关注,我认为我们不能在任何地方定义任何东西?
Thanks in advance..
提前致谢..
采纳答案by Tieson T.
You have at least two options:
你至少有两个选择:
1.) Add a list, array, or any other collection type of cities to your model
1.) 将列表、数组或任何其他城市集合类型添加到您的模型中
2.) Add a SelectList property to your model
2.) 将 SelectList 属性添加到您的模型
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.
选项1可以是一些简单作为一个字符串数组,或者可以是,比方说,一个IEnumerable
的City
对象。然后,您需要将此属性转换SelectListItem
为视图中的对象集合,作为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.
选项 2 的优点是能够直接绑定到DropDownList
,但要求您在操作方法中构造列表。
Then end result is the same, it's just a matter of how pedantic you want to be about SoC.
那么最终结果是一样的,这只是你想对 SoC 有多迂腐的问题。
For example (assuming you add a property called Cities
):
例如(假设您添加了一个名为 的属性Cities
):
@Html.DropDownListFor(m=>m.City, Model.Cities.Select(city => new SelectListItem()
{
Text = city,
Value = city,
Selected = city == Model.City
})
EDIT:
编辑:
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:
为了回答你的评论,我必须做出一些假设。我假设您有一个名为EmployeeModel
. 这个模型有一个属性,City
,它是一个普通的字符串。所以,这是您模型的一部分,因为我认为它是:
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 ...
}
or
或者
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.
此新属性将包含您允许用户从中选择的城市列表。
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.
如果选择第一个选项,则从数据存储区加载 IEnumerable,然后在视图中使用上面的第一个示例,该示例使用 LINQ 将Cities
属性中的每个字符串投影到一个新SelectListItem
对象中。
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:
如果您选择第二个选项,SelectList
则在将模型传递给视图之前在操作中构建一个。这并不是非常困难,因为该类提供了一个构造函数,该构造函数采用IEnumerable
(您的城市列表)和“选定值”,这将是City
属性(请参阅http://msdn.microsoft.com/en-us/库/dd460123%28v=vs.108%29.aspx)。您的代码如下所示:
model.Cities = new SelectList(GetCities(), model.City);
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:
当然,这假设您有一个辅助方法 ( GetCities()
) 从存储城市的任何位置加载它们。您的视图将具有以下内容:
@Html.DropDownListFor(m=>m.City, model.Cities)
The view engine then uses these SelectListItem
s to build the <select>
element and it's <option>
elements.
然后视图引擎使用这些SelectListItem
s 来构建<select>
元素及其<option>
元素。
回答by Phil Cooper
You could have this in your model, it's quickly achieved, although I wouldn't recommend it:
你可以在你的模型中使用它,它很快就实现了,尽管我不推荐它:
public class Place
{
public string City{get;set;}
public SelectListItem[] Cities()
{
return new SelectListItem[2] { new SelectListItem() { Text = "London" }, new SelectListItem() { Text = "New York" } };
}
}
...and your view
...还有你的看法
@Html.DropDownListFor(m => m.City, Model.Cities())
I think the best place for something like this (but is a little more complicated) is your own htmlhelper
and usage could look something like:
我认为最好的地方是这样的(但有点复杂)是你自己的htmlhelper
,用法可能如下所示:
@html.CityDropDownFor(m => m.City)
You could cache the cities nicely and it keeps data and UI work out of your models.
你可以很好地缓存城市,它可以让数据和 UI 工作脱离你的模型。
If you want to learn more about creating your own helpers, I'd suggest a bit of a [read up].1
如果您想了解有关创建自己的助手的更多信息,我建议您阅读一些 [阅读]。1