asp.net-mvc – SelectListItem中的Selected属性永远不会起作用(DropDownList
发布时间:2020-05-22 21:27:56 所属栏目:asp.Net 来源:互联网
导读:我在选择DropDownList的值时遇到问题.我一直在阅读所有类似的帖子,我无法得到解决方案. 实际的方法对我来说似乎非常好,因为我可以检查SelectList中的字段: var selectList = new ListSelectListItem( from variable in someKindOfCollection select new Sele
|
我在选择DropDownList的值时遇到问题.我一直在阅读所有类似的帖子,我无法得到解决方案. 实际的方法对我来说似乎非常好,因为我可以检查SelectList中的字段: var selectList = new List<SelectListItem>(
from variable in someKindOfCollection
select new SelectListItem
{
Selected = variable.Property == selection,Text = variable.Property,Value = variable.Property
});
据说,这给了我完全的控制权.在构建了selectList之后,我可以使用调试器检查变量.一切都很好,其中一个标有“Selected”属性. 然后我使用DropDownListFor来显示视图: @Html.DropDownListFor(
g => g.SomePropertyInModel,selectList,new { @class = "cssClass" })
但它没有用,从来没有…“渲染”下拉列表,但没有选择任何东西. 非常感谢 :) 新例子 @foreach (var loopVariable in Model.Collection)
{
if (Model.SomeCondition != null)
{
selection = someValue;
}
var selectList = new List<SelectListItem>(
from variable in someKindOfCollection
select new SelectListItem
{
Selected = variable.Property == selection,Value = variable.Property
});
@Html.DropDownListFor(
g => g.SomePropertyInModel,new { @class = "cssClass" })
}
那么,selectList是导致行为的局部变量的事实?对不起,我没想到就是这样. 解决方法我认为你遇到了同样的问题.我查看了源代码以找到我的解决方案,看起来这对我来说是一个错误.下面的DropDownListFor应该有帮助,关键是你将选中的值传递给html帮助器.希望这可以帮助.public static class SelectExtensions {
public static IHtmlString DropDownListFor<TModel,TProperty>(this HtmlHelper<TModel> helper,Expression<Func<TModel,TProperty>> expression,IEnumerable<SelectListItem> selectList,string selectedValue,string optionLabel,object htmlAttributes = null) {
return DropDownListHelper(helper,ExpressionHelper.GetExpressionText(expression),selectedValue,optionLabel,htmlAttributes);
}
/// <summary>
/// This is almost identical to the one in ASP.NET MVC 3 however it removes the default values stuff so that the Selected property of the SelectListItem class actually works
/// </summary>
private static IHtmlString DropDownListHelper(HtmlHelper helper,string name,object htmlAttributes) {
name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
// Convert each ListItem to an option tag
var listItemBuilder = new StringBuilder();
// Make optionLabel the first item that gets rendered
if (optionLabel != null)
listItemBuilder.AppendLine(ListItemToOption(new SelectListItem() { Text = optionLabel,Value = String.Empty,Selected = false },selectedValue));
// Add the other options
foreach (var item in selectList) {
listItemBuilder.AppendLine(ListItemToOption(item,selectedValue));
}
// Now add the select tag
var tag = new TagBuilder("select") { InnerHtml = listItemBuilder.ToString() };
tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tag.MergeAttribute("name",name,true);
tag.GenerateId(name);
// If there are any errors for a named field,we add the css attribute
ModelState modelState;
if (helper.ViewData.ModelState.TryGetValue(name,out modelState)) {
if (modelState.Errors.Count > 0)
tag.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
// Add the unobtrusive validation attributes
tag.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name));
return tag.ToHtmlString(TagRenderMode.Normal);
}
private static string ListItemToOption(SelectListItem item,string selectedValue) {
var tag = new TagBuilder("option") { InnerHtml = HttpUtility.HtmlEncode(item.Text) };
if (item.Value != null)
tag.Attributes["value"] = item.Value;
if ((!string.IsNullOrEmpty(selectedValue) && item.Value == selectedValue) || item.Selected)
tag.Attributes["selected"] = "selected";
return tag.ToString(TagRenderMode.Normal);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 为ASP.NET MVC Action Links添加rel和title
- ASP.NET Web Api在返回404时返回200 OK
- asp.net-mvc-3 – 名称’ViewBag’在当前上下文中不存在
- 如何在ASP.NET Membership Cookie中存储自定义数据
- asp.net-mvc-3 – 默认模型绑定器没有绑定到字段的原因是什
- asp.net-mvc – 一个ASP.NET MVC验证器,用于确保至少选中一
- asp.net-mvc – ASP.NET MVC3 IIS无法获取加载样式表
- asp.net-mvc – 为什么不在ASP.NET MVC项目的Content文件夹
- asp.net-mvc – ASP.NET MVC中的Crystal Reports
- asp.net – OWIN第二天拒绝身份验证cookie
推荐文章
站长推荐
- asp.net – 为什么Visual Studio会拒绝访问,尝试
- asp.net – 为什么不能通过IIS7中的web.config删
- asp.net-mvc-5 – 确认邮件中的aspnet身份无效令
- asp.net-mvc – ASP.NET MVC – 无法绑定数组以查
- asp.net-mvc-2 – ASP.MVC 2 RTM ModelState Id属
- asp.net-mvc – 在ASP.NEt MVC 3中传递Html.Begi
- asp.net-mvc – HttpContext中需要什么来允许For
- asp.net-mvc – “无法同时分析32位和64位应用程
- ASP.NET – 如何有效地使用设计模式而不需要过度
- asp.net-mvc – 使用REST API进行身份验证
热点阅读
