C# 如何预选MVC DropDownList for?
我在一个页面上有多个dropdownlists,它们是在运行时构建的。我有一个带有选择列表的视图模型。我尝试过这是伟大的,但它是有点不同,因为我有一个视图上的视图模型的集合。我想我很接近,但我不知道如何正确使用DropDownList for。如何使用不同的值预选每个dropdownlist 虚拟机C# 如何预选MVC DropDownList for?,c#,asp.net,asp.net-mvc,html.dropdownlistfor,C#,Asp.net,Asp.net Mvc,Html.dropdownlistfor,我在一个页面上有多个dropdownlists,它们是在运行时构建的。我有一个带有选择列表的视图模型。我尝试过这是伟大的,但它是有点不同,因为我有一个视图上的视图模型的集合。我想我很接近,但我不知道如何正确使用DropDownList for。如何使用不同的值预选每个dropdownlist 虚拟机 公共类类别 { 公共IEnumerable类别{get;set;} 公共字符串SelectedValue{get;set;} } 看法 @model IEnumerable @f
公共类类别
{
公共IEnumerable类别{get;set;}
公共字符串SelectedValue{get;set;}
}
看法
@model IEnumerable
@foreach(模型中的var项目)
{
//我想我错过了一些东西???
@Html.DropDownListFor(**x=>item.SelectedValue**,item.category)
}
控制器
name是StaticName中的一个,它就像一个Id,它在列表中
var categories = new List<categoryVm>();
var selectlistcolumns = columns.Select(x => new SelectListItem
{
Value = x.StaticName,
Text = x.DisplayName
});
foreach (var column_loopVariable in dtCSV.Columns)
{
var category= new categoryVm();
category.category= selectlistcolumns;
category.SelectedValue = "name";
categories .Add(category);
}
return View(categories);
var categories=新列表();
var selectlistcolumns=columns.Select(x=>newselectListItem
{
Value=x.StaticName,
Text=x.DisplayName
});
foreach(dtCSV.Columns中的var column\u loopVariable)
{
var category=新的categoryVm();
category.category=selectlistcolumns;
category.SelectedValue=“name”;
类别。添加(类别);
}
返回视图(类别);
我更喜欢mvc解决方案,而不是使用javascript更改dom。因为您的控制器返回列表您应该在查看页面中使用IList,而不是IEnumerable。
@Html.DropDownListFor(m => item.SelectedValue, new SelectList(item.category))
@model IList<categoryVm>
for (int i = 0; i < Model.Count; i++)
{
@Html.DropDownListFor(m => Model[i].SelectedValue, Model[i].category)
}
@model-IList
for(int i=0;iModel[i]。SelectedValue,Model[i]。类别)
}
因为您的控制器返回列表您应该在查看页面中使用IList,而不是IEnumerable。
@model IList<categoryVm>
for (int i = 0; i < Model.Count; i++)
{
@Html.DropDownListFor(m => Model[i].SelectedValue, Model[i].category)
}
@model-IList
for(int i=0;iModel[i]。SelectedValue,Model[i]。类别)
}
试试这个
var selectlistcolumns = columns.Select(x => new SelectListItem
{
Value = x.StaticName,
Text = x.DisplayName,
Selected = x.DisplayName == "name" // this sets the selected value
});
foreach (var column_loopVariable in dtCSV.Columns)
{
var category= new categoryVm();
category.category= selectlistcolumns;
//category.SelectedValue = "name"; // not required
categories .Add(category);
}
另外,如果您注意到Html.DropDownListFor(propexpression,selectList)的签名
因此,假设的第一个参数不会设置选定的值。希望这有帮助 试试这个
var selectlistcolumns = columns.Select(x => new SelectListItem
{
Value = x.StaticName,
Text = x.DisplayName,
Selected = x.DisplayName == "name" // this sets the selected value
});
foreach (var column_loopVariable in dtCSV.Columns)
{
var category= new categoryVm();
category.category= selectlistcolumns;
//category.SelectedValue = "name"; // not required
categories .Add(category);
}
另外,如果您注意到Html.DropDownListFor(propexpression,selectList)的签名
因此,假设的第一个参数不会设置选定的值。希望这有帮助 谢谢你的回复,穆拉里。m=>item.category在intellisense中没有SelectedValue。。m=>item已选择了edValue,但这不起作用。@LaurenceNyein,这是我的错误。编辑后的答案。谢谢你的帮助,但我看不出我的原始代码有什么不同。。你能再解释一下吗?谢谢你的回复。m=>item.category在intellisense中没有SelectedValue。。m=>item已选择了edValue,但这不起作用。@LaurenceNyein,这是我的错误。编辑后的答案。谢谢你的帮助,但我看不出我的原始代码有什么不同。。你能再解释一下吗?或者这个方法,或者另一个选项是为“categoryvm”制作一个EditorTemplate,在这种情况下,你可以简单地调用Html.EditorFor,MVC将自动对你的字段应用适当的索引。或者这个方法,或者另一个选项是为“categoryvm”制作一个EditorTemplate,在这种情况下,您只需调用Html.EditorFor,MVC将自动对您的字段应用适当的索引。您可以像预填充文本框值一样预先选择DDL值。有意义吗?可以像预填充文本框值一样预先选择DDL值。有道理?