问题描述
我试图寻找,并没有发现任何固定我的问题。我有,将不显示,我已经标记为选择列表中选择的项目剃刀视图一个DropDownList。这里是控制器code用于填充列表:
I tried searching and didn't find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list:
var statuses = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());
ViewBag.Statuses = statuses;
return View(vm);
下面是查看code:
<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>
我通过它,甚至认为它具有正确的SelectedValue但是走的DDL总是显示在列表中的第一个项目,无论选择的价值。任何人都可以指出我在做什么错,以获得DDL默认的SelectValue?
I walk through it and even in the view it has the correct SelectedValue however the DDL always shows the first item in the list regardless of the selected value. Can anyone point out what I am doing wrong to get the DDL to default to the SelectValue?
推荐答案
的的SelectList
构造函数的最后一个参数(您希望能够通过选择的值因为DropDownListFor助手使用你作为第一个参数传递拉姆达前pression和使用的特定属性的值id)被忽略。
The last argument of the SelectList
constructor (in which you hope to be able to pass the selected value id) is ignored because the DropDownListFor helper uses the lambda expression you passed as first argument and uses the value of the specific property.
因此,这里的丑陋的方式做到这一点:
So here's the ugly way to do that:
型号:
public class MyModel
{
public int StatusID { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: obviously this comes from your DB,
// but I hate showing code on SO that people are
// not able to compile and play with because it has
// gazzilion of external dependencies
var statuses = new SelectList(
new[]
{
new { ID = 1, Name = "status 1" },
new { ID = 2, Name = "status 2" },
new { ID = 3, Name = "status 3" },
new { ID = 4, Name = "status 4" },
},
"ID",
"Name"
);
ViewBag.Statuses = statuses;
var model = new MyModel();
model.StatusID = 3; // preselect the element with ID=3 in the list
return View(model);
}
}
查看:
@model MyModel
...
@Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
和这里的正确方法,采用实视图模型:
and here's the correct way, using real view model:
示范
public class MyModel
{
public int StatusID { get; set; }
public IEnumerable<SelectListItem> Statuses { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: obviously this comes from your DB,
// but I hate showing code on SO that people are
// not able to compile and play with because it has
// gazzilion of external dependencies
var statuses = new SelectList(
new[]
{
new { ID = 1, Name = "status 1" },
new { ID = 2, Name = "status 2" },
new { ID = 3, Name = "status 3" },
new { ID = 4, Name = "status 4" },
},
"ID",
"Name"
);
var model = new MyModel();
model.Statuses = statuses;
model.StatusID = 3; // preselect the element with ID=3 in the list
return View(model);
}
}
查看:
@model MyModel
...
@Html.DropDownListFor(model => model.StatusID, Model.Statuses)
这篇关于MVC DropDownList的SelectedValue的显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!