C# dropdownlist用于不选择什么';s应在页面加载时选择
我有这个助手。这不是我的。这用于显示和填充下拉列表C# dropdownlist用于不选择什么';s应在页面加载时选择,c#,asp.net-mvc,linq,asp.net-mvc-4,C#,Asp.net Mvc,Linq,Asp.net Mvc 4,我有这个助手。这不是我的。这用于显示和填充下拉列表 public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool
public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool> selectedItem = null)
{
return instance.Select(t => new SelectListItem
{
Text = Convert.ToString(text(t)),
Value = Convert.ToString(value(t)),
Selected = selectedItem != null && selectedItem(t)
});
}
然后,当我尝试渲染这些结果以进行编辑/更新时
@model ViewModels.Foo.FooVm
@Html.LabelFor(model => model.FooId)
@foreach(var item in Model.Bars) {
@Html.DropDownListFor(model => item.BarId, ViewBag.Bars as IEnumerable<SelectListItem>, "Select bar")
}
@model ViewModels.Foo.FooVm
@LabelFor(model=>model.FooId)
@foreach(模型条中的var项){
@Html.DropDownListFor(model=>item.BarId,ViewBag.bar作为IEnumerable,“选择栏”)
}
下拉菜单呈现良好,但没有选择页面加载时应该选择的内容
我错过了什么?任何帮助都将不胜感激。谢谢您的助手方法最多需要3个参数,但您只提供了两个。第三个参数允许您在默认情况下设置所选项目,该参数将为“null”。例如,见下文
ViewBag.Bars = Model.Bars.ToSelectList(s => s.BarName, s => s.BarId, s => s.BarId == 1);
编辑
试试这个,但是因为我不理解你的前提或者你真正想要达到的目标,所以这只是一些指导你的东西
public static MvcHtmlString DropDownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,
IEnumerable<SelectListItem> selectList, string selectedValue, string optionLabel, object htmlAttributes)
{
Func<TModel, TValue> method = expression.Compile();
string value = method(helper.ViewData.Model) as string;
if (string.IsNullOrEmpty(value) && selectList != null)
{
var selectListList = selectList.ToList();
var selectedItem = selectListList.FirstOrDefault(s => s.Selected);
if (selectedItem == null)
{
var itemToSelect = selectListList.FirstOrDefault(w => w.Value == selectedValue);
if (itemToSelect != null)
{
itemToSelect.Selected = true;
}
}
return helper.DropDownListFor(expression, selectListList, optionLabel, htmlAttributes);
}
return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}
public static MvcHtmlString DropDownListFor(此HtmlHelper帮助程序、表达式、,
IEnumerable selectList、string selectedValue、string optionLabel、object htmlAttributes)
{
Func method=expression.Compile();
字符串值=方法(helper.ViewData.Model)作为字符串;
if(string.IsNullOrEmpty(value)&&selectList!=null)
{
var selectListList=selectList.ToList();
var selectedItem=selectListList.FirstOrDefault(s=>s.Selected);
如果(selectedItem==null)
{
var itemToSelect=selectListList.FirstOrDefault(w=>w.Value==selectedValue);
if(itemToSelect!=null)
{
itemToSelect.Selected=true;
}
}
return helper.DropDownListFor(表达式、selectListList、optionLabel、htmlAttributes);
}
return helper.DropDownListFor(表达式、selectList、optionLabel、htmlAttributes);
}
像这样使用
@Html.DropDownListFor(model => item, ViewBag.Bars as IEnumerable<SelectListItem>, item.BarId.ToString(), "Select bar", new object())
@Html.DropDownListFor(model=>item,ViewBag.bar作为IEnumerable,item.BarId.ToString(),“Select bar”,new object())
彻底测试这一点,因为这只是一个快速原型,向您展示如何自己创建帮助器
如果您使用第一个答案作为
SelectListItem
的selected
属性,那么所选值可以被覆盖true
我想您可以在razor markup@Html.DropDownListFor(model=>item.BarId,ViewBag.bar作为IEnumerable,“选择栏”)中设置所选值
无论第一个参数中的值是什么,它都会为您设置所选的值。我不能像你给出的那样做,因为它只会将相同的值设置为“所有渲染”下拉列表。助手只是为你提供一个SelectList
,并为你设置列表中的选定项。如果您希望能够直接在@Html.DropDownListFor
中设置值,则需要一个直接执行此操作的帮助程序。在您的上述代码中,@Html.DropDownListFor(model=>item.BarId,ViewBag.bar as IEnumerable,“Select bar”)
item.BarId
实际上什么都不做。你好谢谢你认为你能提供样品解决方案吗?这个助手的实际意义是什么(相对于使用一个SelectList
构造函数,例如newselectlist(_db.bar,“BarId”,“BarName”)
),而您的foreach
循环没有意义-您生成了重复的id
属性(无效html)和重复的name
属性,这些属性不会绑定到与您的模型相关的任何内容。@StephenMuecke非常感谢您。不知道我可以做这样的事情。我的标记已更正。
public static MvcHtmlString DropDownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,
IEnumerable<SelectListItem> selectList, string selectedValue, string optionLabel, object htmlAttributes)
{
Func<TModel, TValue> method = expression.Compile();
string value = method(helper.ViewData.Model) as string;
if (string.IsNullOrEmpty(value) && selectList != null)
{
var selectListList = selectList.ToList();
var selectedItem = selectListList.FirstOrDefault(s => s.Selected);
if (selectedItem == null)
{
var itemToSelect = selectListList.FirstOrDefault(w => w.Value == selectedValue);
if (itemToSelect != null)
{
itemToSelect.Selected = true;
}
}
return helper.DropDownListFor(expression, selectListList, optionLabel, htmlAttributes);
}
return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}
@Html.DropDownListFor(model => item, ViewBag.Bars as IEnumerable<SelectListItem>, item.BarId.ToString(), "Select bar", new object())