Asp.net mvc 3 Html.DropDownList是否有重载,该重载会将模型中的值显示为当前选定的项?
Html.DropDownList是否有重载,该重载会将模型中的值显示为当前选定的项?在我的视图中,我使用as-using语句插入了一个模型Asp.net mvc 3 Html.DropDownList是否有重载,该重载会将模型中的值显示为当前选定的项?,asp.net-mvc-3,Asp.net Mvc 3,Html.DropDownList是否有重载,该重载会将模型中的值显示为当前选定的项?在我的视图中,我使用as-using语句插入了一个模型 @using SchoolIn.Models 然后我访问模型,如下所示: if (Model.Enrollments != null) { @Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().weekDa
@using SchoolIn.Models
然后我访问模型,如下所示:
if (Model.Enrollments != null)
{
@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().weekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
}
@Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<SelectListItem>)ViewData["Categories"]).SetSelected(model.CategoryId))
以下是我的模型中的代码:
public virtual string classDays { get; set; }
public string[] weekDays = new string[6] { "Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
当我的视图加载时,它会显示一个下拉列表,我可以从中选择,我选择一天并保存选择,但当它再次加载时,我希望以前选择的项目成为列表中的默认选择。我该怎么做?我非常感谢您的帮助,谢谢。您可能需要使用DropDownListFor而不是DropDownList。下面是来自的构造函数定义。还有五个过载
public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList
)
public static MvcHtmlString DropDownListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable选择列表
)
看起来您应该能够在适当的项目上设置所选的
:
@{
var Items = Model.Enrollments.FirstOrDefault().weekDays.Select(s =>
new SelectListItem { Text = s.ToString(), Value = s.ToString()
Selected = s.ToString().Equals(PreviouslySelectedValue)
}
);
}
@Html.DropDownList("searchString", Items)
您还可以使用DropDownListFor
,它应该将所选项目设置为属性的值
@Html.DropDownListFor(model => PreviouslySelectedValue, Items)
尝试以下扩展方法:
public static IEnumerable<SelectListItem> SetSelected(this IEnumerable<SelectListItem> selectList, object selectedValue)
{
selectList = selectList ?? new List<SelectListItem>();
if (selectedValue == null)
return selectList;
var vlaue = selectedValue.ToString();
return selectList.BuildList(m => m.Text, m => m.Value, null, m => String.Equals(m.Value, vlaue, StringComparison.CurrentCultureIgnoreCase));
}
public static IEnumerable SetSelected(此IEnumerable selectList,对象selectedValue)
{
选择列表=选择列表??新建列表();
如果(selectedValue==null)
返回选择列表;
var vlaue=selectedValue.ToString();
返回selectList.BuildList(m=>m.Text,m=>m.Value,null,m=>String.Equals(m.Value,vlaue,StringComparison.CurrentCultureIgnoreCase));
}
那么你可以这样称呼它:
if (Model.Enrollments != null)
{
@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().weekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
}
@Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<SelectListItem>)ViewData["Categories"]).SetSelected(model.CategoryId))
@Html.DropDownListFor(model=>model.CategoryId,((IEnumerable)ViewData[“Categories”]).SetSelected(model.CategoryId))
您将所选内容保存到哪里?谢谢,这已经奏效,但现在又导致了另一个问题……我在视图中有一系列复选框,当我将所选值放入其中时,它们都具有相同的值。你能帮我吗,或者给我指一个可能对我有帮助的教程?谢谢。