我的下拉列表不想有默认值!
<div class="form-group">
@Html.LabelFor(model => model.unit, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.unit.id, selectUnit)
@Html.ValidationMessageFor(model => model.unit.id, "", new { @class = "text-danger" })
</div>
</div>
显示正确的列表,但没有选择任何一个。
我使用 ViewBag 获取 SelectList:
@{
IEnumerable<SelectListItem> selectUnit = ViewBag.Unit;
}
当我在 cshtml 中断点时,Model.unit.id 为 4,并且 selectUnit 有一项值为 4。
当我这样做的时候
@selectUnit.Where(x => x.Value == Model.unit.id.ToString()).First().Text
它选择正确的文本值!
拉特认为:这是我的单位模型:
public class Unit
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public IList<Unit> children { get; set; }
}
先谢谢大家了,我快疯了
编辑:
public class ModelPassedTroughTheView
{
...
public Unit unit { get; set; }
}
编辑2:完整代码:
编辑页面: @model BE.DealerGroupSAP
@{
ViewBag.Title = Resources.Admin.DealerGroup_Edit;
IEnumerable<SelectListItem> selectUnit = ViewBag.Unit;
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>@ViewBag.Title</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
<div class="form-group">
@Html.LabelFor(model => model.unit, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.unit.id, selectUnit)
@Html.ValidationMessageFor(model => model.unit.id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="@Resources.Global.Save_Edits" class="btn btn-default" />
</div>
</div>
</div>
}
模型路人槽 View :
public class DealerGroupSAP
{
public int id { get; set; }
public Unit unit { get; set; }
}
单位对象:
public class Unit
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public IList<Unit> children { get; set; }
}
Controller 的内容:
ViewBag.Unit = GetUnits();
return View(BL.AnomalyBL.GetAllSAPResponsible(id));
最佳答案
问题是您的模型有一个名为 unit
的属性并且您还通过了 SelectList
查看ViewBag
属性名为 Unit
(MVC 的模型绑定(bind)功能不区分大小写。
更改 ViewBag
的名称属性(property)(比如说)
ViewBag.UnitList = GetUnits();
并在 View 中
@{ IEnumerable<SelectListItem> selectUnit = ViewBag.UnitList }
并且将选择正确的选项。
解释内部发生的事情:
DropDownListFor()
方法确定 defaultValue
(所选项目)首先检查 ModelState
中的值(在您的情况下不存在),然后检查 ViewData
。因为ViewData
包含 Unit
的键/值对,即IEnumerable<SelectListItem>
并且不包含属性 id
,defaultValue
是 null
该方法使用 IEnumerable<SelectListItem>
您传递到 View 来构建 <option>
元素,其中没有一个 Selected = true
值,因此选择第一个选项,因为必须这样做。
更改ViewBag
属性(property)至(例如)UnitList
表示该方法未找到 unit
的匹配键在ViewData
现在检查模型的 unit.id
,存在并设置 defaultValue = 4
。因为defaultValue
不是null
,一个新的IEnumerable<SelectListItem>
内部生成,对应SelectListItem
有其Selected
属性设置为true
.
要了解这一切的详细工作原理,您可以检查 SelectExtensions 的源代码- 特别是private static MvcHtmlString SelectInternal()
方法。
最后一点,这只是您应该始终使用 View 模型的又一个原因。
关于c# - DropDownList 不想被设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41849481/