Html.DropDownList用于设置所选值 [英] Html.DropDownListFor set selected value
问题描述
我创建一个@ Html.DropDownListFor并从数据库中填充它.如何为下拉菜单设置选定的值?
I create a @Html.DropDownListFor and populate it from the database. How can I set a selected value to the drop down?
我的观点:
@Html.DropDownListFor(m => m.Forms, new SelectList(Model.Forms, "FormsCreatorID", "FormName"),
"Select a Form", new { @class = "form-control" })
我的控制器:
var forms = db.formscreators.Where(fp => fp.PropertyID == id || fp.PropertyID == 0)
.OrderByDescending(x => x.PropertyID).GroupBy(x => x.FormName).Select(x => x.FirstOrDefault()).ToList();
var viewModel = new ListFormsCreator { Forms = forms };
我的ViewModel:
My ViewModel:
public class ListFormsCreator
{
public List<formscreator> Forms { get; set; }
}
我的数据库模型:
public partial class formscreator
{
public int FormsCreatorID { get; set; }
public string FormName { get; set; }
public int PropertyID { get; set; }
}
谢谢
推荐答案
您应该为存储/传递所选选项的视图模型添加另一个属性.
You should add another property to your view model for the store/pass the selected option.
public class ListFormsCreator
{
public int SelectedFormId { set;get;}
public List<formscreator> Forms { get; set; }
}
现在在GET操作中,您可以设置该值
Now in your GET action, you can set that value
var viewModel = new ListFormsCreator() { Forms = forms };
viewModel.SelectedFormId = 2 ; // This will select the option with 2 as FormsCreatorID
return View(viewModel);
然后在视图中使用具有该属性的lamda表达式作为 DropDownListFor
帮助器方法的第一个参数.
And in the view use the lamda expression with that property as the first parameter of the DropDownListFor
helper method.
@model ListFormsCreator
@Html.DropDownListFor(m => m.SelectedFormId ,
new SelectList(Model.Forms, "FormsCreatorID", "FormName"),
"Select a Form", new { @class = "form-control" })
DropDownListFor
辅助方法将使用 SelectedFormId
属性的值,并从以下列表中选择具有相同 value
属性值的选项:该SELECT元素的选项.
The DropDownListFor
helper method will use the value of SelectedFormId
property and select the option which has the same value
attribute value from the list of options of that SELECT element.
您也可以从视图模型中删除对 formscreator
类的依赖,方法是将其替换为 SelectListItem
You can also remove the dependency on formscreator
class from the view model, by replacing it with a list of SelectListItem
public class ListFormsCreator
{
public int SelectedFormId { set;get;}
public List<SelectListItem> Forms { get; set; }
}
现在,在GET操作中,可以使用 Select
方法从其他集合中生成 SelectListItem
的信息.
Now in your GET action, you can use the Select
method to generate the lsit of SelectListItem
from your other collection.
var viewModel = new ListFormsCreator();
viewModel.Forms = someCollection.Select(a=>new SelectListItem {
Value=a.FormsCreatorId.ToString(),
Text=a.FormName})
.ToList();
viewModel.SelectedFormId = 2 ; // This will select the option with 2 as FormsCreatorID
return View(viewModel);
假设 someCollection
是 formscreator
对象的集合现在,在视图中的代码要简单得多
Assuming someCollection
is a collection of formscreator
objects
Now in the view code is much simpler
@Html.DropDownListFor(m => m.SelectedFormId, Model.Forms ,"Select a Form")
这篇关于Html.DropDownList用于设置所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!