C# 使用默认值从SelectList创建DropDownList for

C# 使用默认值从SelectList创建DropDownList for,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有一个的下拉列表: @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" }) @Html.ValidationMessageFor(model => model.Item.Item.Status) HTML输出: <select id="status

我有一个的下拉列表:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
 @Html.ValidationMessageFor(model => model.Item.Item.Status)
HTML输出:

<select id="statusDropdown" class="valid" name="Item.Item.Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">
<option value="2">Completed by Admin</option>
<option value="3">General Error</option>
<option value="4">New</option>
</select>
@Model.SelectedStatusIndex的值为4,但不会将默认选项更改为New

我也试过:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description",@Model.SelectedStatusIndex), new { id = "statusDropdown" })
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
@Html.ValidationMessageFor(model => model.Item.Item.Status)
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" })
这将选择默认选项“新建”,但HTTP POST上的下拉列表不会设置
model.Item.Item.Status

其他详细信息:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description",@Model.SelectedStatusIndex), new { id = "statusDropdown" })
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
@Html.ValidationMessageFor(model => model.Item.Item.Status)
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" })

model.Item.Item.Status
是一个int.
@model.AllStatus
是一个SQL表,列出了所有可用的状态选项。

已经有一些关于的讨论。其中一个问题可能是对键值使用了与
string
不同的类型。我过去也遇到过类似的问题,我知道我解决了它,就像在准备列表时显式设置
Selected
属性一样(在您的例子中,
AlLStatus

就您的情况而言,这意味着(在控制员行动中):

IEnumerable selectList=
从allStatus中的s//从何处获取此信息,数据库等。
选择新的SelectListItem
{
所选=(s.id==model.Item.Item.Status),
Text=cs.说明,
Value=s.id.ToString()
};
model.AllStatus=选择列表;

我最终使用了托马斯贾沃斯基答案的变体

查看:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description",@Model.SelectedStatusIndex), new { id = "statusDropdown" })
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
@Html.ValidationMessageFor(model => model.Item.Item.Status)
@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" })
视图模型构造函数

        StatusSelectList = AllStatus.Select(x =>
                                        new StatusSelectListItem
                                        {
                                            Text = x.Description,
                                            Value = x.id.ToString()
                                        }).ToList();

        this.SelectedStatusIndex = 2;//Default Status is New
HTTP POST上的控制器

我将
model.Item.Item.Status
与下拉列表本身分开设置:

model.Item.Item.Status = model.SelectedStatusIndex;
因为下拉列表集是作为第一个参数传递的表达式的值:

@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" })

在本例中,
model.SelectedStatusIndex
是下拉列表设置的内容。这个控制器的实现是我发现的棘手问题。

这是对上述答案的补充。我会这样做的

视图模型用于表示您的数据。因此,对于单个下拉列表,我将有以下内容:

public class MyViewModel
{
     public int StatusId { get; set; }

     public IEnumerable<Status> Statuses { get; set; }
}
控制器处理视图的操作方法:

public class MyController
{
     private readonly IStatusService statusService;

     public MyController(IStatusService statusService)
     {
          this.statusService = statusService;
     }

     public ActionResult MyActionMethod()
     {
          MyViewModel viewModel = new MyViewModel
          {
               Statuses = statusService.GetAll(),
               StatusId = 4 // Set the default value
          };

          return View(viewModel);
     }
}
视图将如下所示:

public class Status
{
     public int Id { get; set; }

     public string Description { get; set; }
}
@model MyProject.ViewModels.MyViewModel

@Html.DropDownListFor(
     x => x.StatusId,
     new SelectList(Model.Statuses, "Id", "Description", Model.StatusId),
     "-- Select --"
)
@Html.ValidationMessageFor(x => x.StatusId)
好了。

您可以使用“插入”添加下拉列表的默认值,并将其添加到动态列表中: 通过这种方式,你不需要在你的视野中使用剃须刀

List<Y> m = X.GetList();
m.Insert(0, new Y{ Name = "--Select--" });
List m=X.GetList();
m、 插入(0,新Y{Name=“--Select-->”);

我使用列表中已定义的值分配DropDownList for的表达式。它对我有用。我使用
列表模型.IncidentPriorityList
作为ddwn的selectlist

Model.incident.INCIDENT_PRIORITY_ID = Model.DefaultParameterId; @Html.DropDownListFor(m => m.incident.INCIDENT_PRIORITY_ID, Model.IncidentPriorityList, "Select", new { @class = "form-control selectpicker", id = "drpPriority", @required = true }) Model.incident.incident\u PRIORITY\u ID=Model.DefaultParameterId;
@Html.DropDownListFor(m=>m.incident.incident_PRIORITY_ID,Model.IncidentPriorityList,“Select”,new{@class=“form control selectpicker”,ID=“drpPriority”,@required=true})感谢您发布解决方案!注意:如果您使用@Html.DropDownListFor(model=>model.SelectedStatusIndex,new SelectList(@model.StatusSelectList,“Value”,“Text”)),则选择列表的id将被SelectedStatusIndex。。。。此外,如果您使用@Html.DropDownListFor(model=>model.SelectedStatusIndex,new SelectList(@model.StatusSelectList,“Value”,“Text”),“Please Select”),它会将Please Select作为第一个条目。是的,但是如何将“Please Select”赋值为-1?