gpt4 book ai didi

asp.net-mvc - SelectListItem 中的选定属性永远不起作用 (DropDownListFor)

转载 作者:行者123 更新时间:2023-12-02 16:16:33 25 4
gpt4 key购买 nike

我在选择 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 后,我​​可以使用调试器检查变量。一切正常,其中之一已标记为“选定”属性。

然后我使用 DropDownListFor 以便在 View 上显示:

@Html.DropDownListFor(
g => g.SomePropertyInModel , selectList, new { @class = "cssClass" })

但是它不起作用,永远不会...“渲染”下拉列表,但没有选择任何内容。

非常感谢:)

新示例首先我想道歉。我一直在隐藏信息,当然完全是无意的。所有代码都发生在 Razor For 循环内:

@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,
Text = variable.Property,
Value = variable.Property
});

@Html.DropDownListFor(
g => g.SomePropertyInModel , selectList, 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), selectList, 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, IEnumerable<SelectListItem> selectList, string selectedValue, string optionLabel, 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 - SelectListItem 中的选定属性永远不起作用 (DropDownListFor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9792581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com