C# DropDownList用于内部编辑器,用于不选择值
我发现了一些关于我的问题的问题,但没有一个真正解决了问题,并给出了问题的替代解决方案,这就是为什么我再次提出这个问题 我使用的是强类型HTML帮助程序,没有ViewData和ViewBag,只有页面标题 这是我的问题 我有以下的视图模型C# DropDownList用于内部编辑器,用于不选择值,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我发现了一些关于我的问题的问题,但没有一个真正解决了问题,并给出了问题的替代解决方案,这就是为什么我再次提出这个问题 我使用的是强类型HTML帮助程序,没有ViewData和ViewBag,只有页面标题 这是我的问题 我有以下的视图模型 public class RegisterViewModel { public string Mail { get; set; } public string Name
public class RegisterViewModel
{
public string Mail { get; set; }
public string Name { get; set; }
public ThreePartDatePickerViewModel ThreePartDateSelection { get; set; }
public RegisterViewModel()
{
ThreePartDateSelection = new ThreePartDatePickerViewModel();
}
}
上面的viewmodel使用下面的viewmodel,它基本上保存了3个下拉列表的数据,即日、月和年
public class ThreePartDatePickerViewModel
{
public string Day { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public IList<SelectListItem> Years { get; set; }
public IList<SelectListItem> Months { get; set; }
public IList<SelectListItem> Days { get; set; }
public ThreePartDatePickerViewModel()
{
var now = DateTime.Now;
Years = new List<SelectListItem>();
Months = new List<SelectListItem>();
Days = new List<SelectListItem>();
var empty = new SelectListItem { Text = "" };
Years.Add(empty);
Months.Add(empty);
Days.Add(empty);
foreach(var item in Enumerable.Range(0, 100).Select(x => new SelectListItem { Value = (now.Year - x).ToString(), Text = (now.Year - x).ToString() }))
Years.Add(item);
foreach(var item in Enumerable.Range(1, 12).Select(x => new SelectListItem { Value = x.ToString(), Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x) }))
Months.Add(item);
foreach(var item in Enumerable.Range(1, 31).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }))
Days.Add(item);
}
}
我的三部分DatePickerViewModel编辑器模板是
@model Bla.Bla.ThreePartDatePickerViewModel
<div class="threePartDatePicker">
@Html.DropDownListFor(m => m.Day, Model.Days)
@Html.DropDownListFor(m => m.Month, Model.Months)
@Html.DropDownListFor(m => m.Year, Model.Years)
</div>
@model Bla.Bla.ThreePartDatePickerViewModel
@DropDownListFor(m=>m.Day,Model.Days)
@DropDownListFor(m=>m.Month,Model.Month)
@Html.DropDownListFor(m=>m.Year,Model.Years)
在最终呈现的html中,我拥有了预期的所有控件。下拉列表渲染良好,但我在action method中设置的值未选中
如果我从EditorTemplates切换到部分视图,它就会开始工作。或者,如果直接在我的主视图中呈现DropDownList,而不是将模型传递给EditorFor,它会再次工作。这似乎是一个老错误,尚未解决。据此间报道 我必须编辑编辑器模板以将选定值设置为
@{
//capture the selected list wherever it is. Here it passed in additionalViewData
SelectList tmpList, list = null;
ViewContext.ViewData.TryGetValue("list", out tmpList);
if (tmpList != null && tmpList.Count() > 0 && tmpList.SelectedValue == null)
{
list = new SelectList(tmpList, "Value", "Text", Model);
}
else
{
list = tmpList;
}
}
<div class="form-group">
@Html.DropDownListFor(m => m, list, attributes)
</div>
@{
//无论所选列表位于何处,都可以捕获它。在这里,它传递了额外的ViewData
选择list tmpList,list=null;
ViewContext.ViewData.TryGetValue(“列表”,out-tmpList);
如果(tmpList!=null&&tmpList.Count()>0&&tmpList.SelectedValue==null)
{
列表=新的选择列表(tmpList,“值”、“文本”、型号);
}
其他的
{
列表=tmpList;
}
}
@DropDownListFor(m=>m,列表,属性)
因此,我可以使用编辑器模板中的选择列表作为默认行为。在编辑器模板中使用下拉列表时,我遇到了相同的问题。即使我可以在模型中看到正确的值,也未设置选定的值。这是我的解决办法。它可以扩展到其他
DropdownListFor
方法
public static class HtmlHelperExtensions
{
public static IHtmlString EditorDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
{
var dropDown = SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel);
var model = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;
if (model == null)
{
return dropDown;
}
var dropDownWithSelect = dropDown.ToString().Replace("value=\"" + model.ToString() + "\"", "value=\"" + model.ToString() + "\" selected");
return new MvcHtmlString(dropDownWithSelect);
}
}
公共静态类HtmlHelperExtensions
{
公共静态IHtmlString EDITORDROPPDOWNLISTFOR(此HtmlHelper HtmlHelper、表达式表达式、IEnumerable selectList、字符串选项标签)
{
var dropDown=SelectExtensions.DropDownListFor(htmlHelper、表达式、selectList、optionLabel);
var model=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData).model;
if(model==null)
{
返回下拉列表;
}
var dropDownWithSelect=dropDown.ToString().Replace(“value=\”“+model.ToString()+”,“value=\”“+model.ToString()+”selected”);
返回新的MvcHtmlString(dropDownWithSelect);
}
}
好心的,你有关于这个问题的任何信息/解决方案吗?@Shady很遗憾,我没有。我用了局部视图,谢谢。似乎它仍然是一个bug。
public static class HtmlHelperExtensions
{
public static IHtmlString EditorDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
{
var dropDown = SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel);
var model = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;
if (model == null)
{
return dropDown;
}
var dropDownWithSelect = dropDown.ToString().Replace("value=\"" + model.ToString() + "\"", "value=\"" + model.ToString() + "\" selected");
return new MvcHtmlString(dropDownWithSelect);
}
}