相关疑难解决方法(0)

DropDownListFor不选择值

我正在编辑页面中使用DropDownListFor帮助方法,我没有运气来选择我指定的值.我在Stackoverflow上发现了一个类似的问题.建议的解决方法是"在视图代码中填充SelectList".问题是我已经尝试过这个但它仍然没有用.

<%= Html.DropDownListFor(model => model.States, new SelectList(Model.States.OrderBy(s => s.StateAbbr), "StateAbbr", "StateName", Model.AddressStateAbbr), "-- Select State --")%>
Run Code Online (Sandbox Code Playgroud)

我设置了断点并验证了model.AddressStateAbbr的存在性(和有效性).我只是不确定我错过了什么.

asp.net-mvc

76
推荐指数
4
解决办法
8万
查看次数

在EditorTemplate中DropDownListFor没有选择值

我有一个自定义对象的编辑器模板.在该编辑器模板中,我使用了几个DropDownListFor帮助器.在每个中我指定一个唯一的模型属性(具有预先选择的值)和包含所有选择选项的选择列表.

例:

<%=Html.DropDownListFor(m => m.DocumentCategoryType, Model.DocumentCategoryTypeList) %>
Run Code Online (Sandbox Code Playgroud)

我知道正在填充选项值(来自查看源),并且我的模型使用正确的ID值(DocumentCategoryType)传入.

渲染视图时,我的下拉列表中没有选定的项目,因此默认为第一个(未选中)值.

有没有人有任何想法?

谢谢.

asp.net-mvc

50
推荐指数
4
解决办法
4万
查看次数

MVC DropDownList SelectedValue无法正确显示

我试过搜索,没有发现任何解决我问题的方法.我在Razor视图上有一个DropDownList,它不会显示我在SelectList中标记为Selected的项目.以下是填充列表的控制器代码:

var statuses  = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());
ViewBag.Statuses = statuses;
return View(vm);
Run Code Online (Sandbox Code Playgroud)

这是查看代码:

<div class="display-label">
   Order Status</div>
<div class="editor-field">
   @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
   @Html.ValidationMessageFor(model => model.StatusID)
</div>
Run Code Online (Sandbox Code Playgroud)

我遍历它,甚至在视图中它具有正确的SelectedValue但是DDL始终显示列表中的第一个项目,而不管选择的值如何.任何人都可以指出我做错了让DDL默认为SelectValue吗?

asp.net-mvc html.dropdownlistfor asp.net-mvc-3

20
推荐指数
1
解决办法
4万
查看次数

ASP.NET MVC下拉列表未在render上选择值

我有这个恼人的问题,我的DropDownlist默认情况下不选择当前值.

控制器:

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;
Run Code Online (Sandbox Code Playgroud)

视图:

<%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %>
Run Code Online (Sandbox Code Playgroud)

但它不是选择"5-10yrs",而是显示"-select-"选项,如果我检查DOM源,则不会选择任何元素.

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

<%
    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>
Run Code Online (Sandbox Code Playgroud)

这不是最好的解决方案,但如果你因为你一直在拉扯你的头发来解决这个问题,这应该会有所帮助.

asp.net-mvc

9
推荐指数
4
解决办法
2万
查看次数

Html.DropDownList选择的值不能使用ViewBag

好吧,经过几个小时阅读这里的东西,尝试所有的解决方案失败,也发现这篇文章,我认为这将拯救我的生命......没有.

长话短说.

这是我的观点(所有组合)

@Html.DropDownList("yearDropDown",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDownxxx",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown",(<SelectList>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown")
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

public ActionResult(int year)
{
var years = new int[] { 2007, 2008, 2009, 2010, 2011, 2012 }
                .Select(x => new SelectListItem { 
                    Text = x.ToString(), 
                    Value = x.ToString(), 
                    Selected=x==year }).Distinct().ToList();

            years.Insert(0, new SelectListItem { Value = null, Text = "ALL YEARS" });
            ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
return View();
}
Run Code Online (Sandbox Code Playgroud)

这是我呈现的HTML.选择无处可寻.

<select id="yearDropDown" name="yearDropDown"><option value="">ALL YEARS</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option> …
Run Code Online (Sandbox Code Playgroud)

html-select selectedvalue razor asp.net-mvc-3

5
推荐指数
2
解决办法
3万
查看次数

DropDownList不选择SelectList中的选定项

我正在使用ASP.NET MVC3应用程序,我无法在特定的编辑器视图中使用DropDownListFor来处理我的属性.

我的模型是a Person和一个人有一个属性,指定它的"人物类型".PersonType是一个包含名称/描述和ID的类.我可以PersonType通过我的静态/共享类访问应用程序中的可用s ApplicationSettings.

在我的编辑模板视图中,我Person创建了一个SelectList用于调试的目的:

@ModelType MyNamespace.Person
@Code
    Dim pTypesSelectList As New SelectList(MyNamespace.ApplicationSettings.PersonTypes, "ID", "Name", Model.PersonType.ID)
End Code
Run Code Online (Sandbox Code Playgroud)

然后我提供这个SelectList作为参数DropDownListFor绑定到PersonType我的属性Person.

我也在打印Selected每个项目的属性以SelectList进行调试:

<div style="text-align: center; margin: 5px 0 0 0;">
    <div>
        @Html.LabelFor(Function(model) model.PersonType)
    </div>
    <div>
        @Html.DropDownListFor(Function(model) model.PersonType, pTypesSelectList)
        @Html.ValidationMessageFor(Function(model) model.PersonType)

        <br />
        <br />
        <!-- The following is debugging code that shows the actual value-->
        @Model.Type.Name
        <br />
        @Model.Type.ID …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc selectlist html.dropdownlistfor asp.net-mvc-3

3
推荐指数
1
解决办法
3984
查看次数