asp.net-mvc ASP.NET MVC 下拉列表未选择渲染值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14604856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:34:12  来源:igfitidea点击:

ASP.NET MVC dropdownlist not selecting value on render

asp.net-mvc

提问by Captain Kenpachi

I have this annoying problem where my DropDownlist doesn't select the current value by default.

我有一个烦人的问题,我的 DropDownlist 默认不选择当前值。

Controller:

控制器:

var YearsCycling = new SelectList(new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
},
"Value",
"Text",
new SelectListItem() { Value = "5-10yrs", Text = "5-10yrs", 
Selected = true });
ViewBag.YearsCycling = YearsCycling;

View:

看法:

<%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %>

but instead of selecting "5-10yrs", it just shows the "-select-" option and if I inspect the DOM source, none of the elements are selected.

但它没有选择“5-10yrs”,而是只显示“-select-”选项,如果我检查 DOM 源,则不会选择任何元素。

UPDATE: I still don't know what the problem is, but I got it working for now by doing something ugly:

更新:我仍然不知道问题是什么,但我现在通过做一些丑陋的事情让它工作:

<%
    var sl = (SelectList)ViewBag.YearsCycling;
%>
<select name="YearsCycling" id="YearsCycling">
<%foreach(var li in sl){  %>
    <option value="<%:li.Value %>" <%if(li.Selected){%>selected="true"<%}%>><%:li.Text %></option>
<%} %>
</select>

This isn't the best solution, but if you've come to this question because you've been pulling your hair out, this should help.

这不是最好的解决方案,但如果你因为一直在拔头发而遇到这个问题,这应该会有所帮助。

采纳答案by scartag

Your code should be.

你的代码应该是。

var YearsCycling = new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs", Selected=true}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
};

ViewBag.YearsCycling = YearsCycling;

You could use this overload instead.

您可以改用此重载。

<%:Html.DropDownListFor(model=>model.YearsCycling,ViewBag.YearsCycling as List<SelectListItem>) %>

回答by Brian

I'm sure you're way past this, but I just got burned on this a moment ago because I had named the dropdown list the same name as the property in my view model class.

我敢肯定您已经过了这一点,但我刚才对此感到很兴奋,因为我已将下拉列表命名为与视图模型类中的属性相同的名称。

Changing the dropdown list name to something else cured it right up.

将下拉列表名称更改为其他名称即可解决问题。

回答by Kapil Khandelwal

Try this:

尝试这个:

Create a viewmodel:

创建一个视图模型:

public class MyViewModel
{
    public string SelectedValue { get; set; }
    public IEnumerable<SelectListItem> YearsCycling { get; set; }
}

Controller:

控制器:

var YearsCycling = new SelectList(new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
},
"Value",
"Text");

MyViewModel model = new MyViewModel();
model.YearsCycling = YearsCycling;
model.SelectedValue = "5-10yrs";
return View(model);

View:

看法:

<%:Html.DropDownListFor(model=>model.SelectedValue,(SelectList)Model.YearsCycling) %>

回答by Kapil Khandelwal

If you are rendering your list like this: @Html.DropDownListFor(model => model.EntityID, Model.EntitySelectList)

如果您像这样呈现您的列表: @Html.DropDownListFor(model => model.EntityID, Model.EntitySelectList)

The value .NET MVC will use when rendering the select options will match the value of model.EntityID.ToString().

.NET MVC 在呈现选择选项时将使用的值将匹配 .NET MVC 的值model.EntityID.ToString()

... It will not select the Model.EntitySelectList.SelectedValue

...它不会选择 Model.EntitySelectList.SelectedValue

... Or any of the EntitySelectList.Item.SelectedValueitems.

...或任何EntitySelectList.Item.SelectedValue项目。

... In other words, when rendering a DropDownListFor something other than the list itself, the SelectedValue properties of both the Listand the List Itemsare ignored.

... 换句话说,当呈现 DropDownListFor列表本身以外的内容时,列表列表项的 SelectedValue 属性都将被忽略。

No matter what Model.EntitySelectList.SelectedValue or Model.EntitySelectList.Item.SelectedValue is set, ASP.NET MVC will not render selection. Use the model.EntityIDinstead.

无论 Model.EntitySelectList.SelectedValue 或 Model.EntitySelectList.Item.SelectedValue 设置什么,ASP.NET MVC 都不会呈现选择。使用model.EntityID来代替。

回答by dnxit

Binding dropdownlist is very tricky in MVC it embarrassed me a lot you can do it with this in your controller get all your cities list put it in viewBag

绑定下拉列表在 MVC 中非常棘手,这让我很尴尬,您可以在控制器中使用此操作将所有城市列表放入 viewBag

Create

创建

      ViewBag.CityId = new SelectList(db.Cities, "ID", "Name");

user.CityID if you are in Edit so that on edit it select the city

user.CityID 如果您在编辑中,以便在编辑时选择城市

      ViewBag.CityId = new SelectList(db.Cities, "ID", "Name", user.CityID);

in your View just do this trick

在您的视图中只需执行此技巧

     @Html.DropDownList("CityId", "Select")

this is the most simplest way I know....

这是我所知道的最简单的方法......

回答by NicoJuicy

I have some problems when using a ViewBag with the same name as the html-element.

使用与 html 元素同名的 ViewBag 时遇到一些问题。

So, doesn't use a DropDownList for EventId, when the SelectList is stored in a ViewBag.EventId.

因此,当 SelectList 存储在 ViewBag.EventId 中时,不为 EventId 使用 DropDownList。

Change ViewBag.EventId to ViewBag.EventData, and it's fixed.

把ViewBag.EventId改成ViewBag.EventData,就修复了。

回答by Lee Cordell

Just ensure that the variable name set on ViewBag is different to the name assigned to the dropdownlist - i.e. don't do:

只需确保在 ViewBag 上设置的变量名称与分配给下拉列表的名称不同 - 即不要这样做:

Controller:

控制器:

ViewBag.CityId = new SelectList(....)

ViewBag.CityId = new SelectList(....)

View:

看法:

@Html.DropDownList("CityId", ...

@Html.DropDownList("CityId", ...

Just use a different viewbag variable name.

只需使用不同的 viewbag 变量名称。

I've had problems when using the same ViewBag variable name as a html control name with drop down lists and listboxes.

在使用相同的 ViewBag 变量名称作为带有下拉列表和列表框的 html 控件名称时,我遇到了问题。

回答by Imaginary

Also Html.DropDownListFor uses values from POST request.

此外 Html.DropDownListFor 使用来自POST 请求的值。

Therefore if you have the same parameter in POST data MVC will use it even if you have different value in model.

因此,如果您在 POST 数据中有相同的参数,即使模型中有不同的值,MVC 也会使用它。

Check if you have the same name in request and change it.

检查您是否在请求中具有相同的名称并进行更改。