C# MVC SelectList将随机值设置为“选定”
我有一个硬编码的选择列表,从数据库中提取值和文本。 我有一个特定的项要设置为默认项,所以我对列表进行排序,以便将其具体地放在Wip行的第一位。但是,当列表以HTML呈现时,列表的顺序是正确的,但是所选的值是具有最小值的项,即使它由于降序而位于列表的底部 型号: 视图: 列表在视图中以Wip作为第一个选择进行渲染,但“渲染选定项=选定项”选项是id为0的项 我知道我可以在数据访问方法中设置“Selected”值,但是我不能在已经具有选定值的编辑表单中使用该值 更新-呈现的HTML:C# MVC SelectList将随机值设置为“选定”,c#,asp.net-mvc,selectlist,C#,Asp.net Mvc,Selectlist,我有一个硬编码的选择列表,从数据库中提取值和文本。 我有一个特定的项要设置为默认项,所以我对列表进行排序,以便将其具体地放在Wip行的第一位。但是,当列表以HTML呈现时,列表的顺序是正确的,但是所选的值是具有最小值的项,即使它由于降序而位于列表的底部 型号: 视图: 列表在视图中以Wip作为第一个选择进行渲染,但“渲染选定项=选定项”选项是id为0的项 我知道我可以在数据访问方法中设置“Selected”值,但是我不能在已经具有选定值的编辑表单中使用该值 更新-呈现的HTML: 所以你可能不喜
所以你可能不喜欢这个答案,但让我简单地概括一下。您的选择列表不应属于您的型号。从“选择此项”中选择的值应该是模型的一部分。您在模型中投入了太多的开销,这对您的视图产生了影响。重构它。将选择列表放在ViewBag中,以便在视图中使用,停止来回传递。这将使您的模型紧凑,并允许您正确设置选项 下面是一个简单的例子 您的数据访问将是什么?例如,为了简单起见,将其删除,并将其设置为静态:
public static IEnumerable<SelectListItem> PopulateBinList()
{
return new List<SelectListItem>{ new SelectListItem {
Text = "WIP", Value = "582"
}, new SelectListItem {
Text = "0888", Value = "595"
} , new SelectListItem {
Text = "0", Value = "0919"
}, new SelectListItem {
Text = "1", Value = "1"
}};
}
在控制器中
public ActionResult Index()
{
//load up your select list into the view bag
ViewBag.BinOptions = ViewHelper.PopulateBinList();
//preset the value of your desired object
var bm = new BinModel { BinId = "582" };
return View(bm);
}
在视图中,您将绑定到模型属性,但使用视图包中定义的临时列表填充选项。预设模型的特性将自动选择“选择”列表中的默认值:
@Html.DropDownListFor(x => x.BinId, (IEnumerable<SelectListItem>)ViewBag.BinOptions, null, new { @class ="form-control" })
如果这不合理或需要扩展,请告诉我。我非常确定BindId等于0。如果BindId是一个简单的整数,那么默认情况下它将始终等于0。您需要自己将其设置为582,还是将其设置为整数?调用Html.DropDownListFor时model.BinId的值是多少?你能给出html.DropDownListFor生成的html吗?@the_lotus-不确定如何找到BinId值,但我假设它是0。我已经添加了呈现的HTML。我一直在处理排序,然后改为降序,但没有改变,它仍然将值为0的项设置为选中项。我非常确定BindId等于0。如果BindId是一个简单的整数,那么默认情况下它将始终等于0。您需要自己将其设置为582,还是将其设置为整数?这会将默认值设置为null。@这是有意义的。好的,谢谢。
@Html.DropDownListFor(model => model.BinId, Model.BinOptionsList, new { @class = "form-control" })
<select class="form-control" data-val="true" data-val-number="The field Bin Code must be a number." data-val-required="The Bin Code field is required." id="BinId" name="BinId">
<option value="582">WIP</option>
<option value="595">0888</option>
<option selected="selected" value="0">0919</option>
<option value="1">1</option>
<option value="2">1A1</option>
<option value="3">1A10</option>
<option value="4">1A11</option>
</select>
public static IEnumerable<SelectListItem> PopulateBinList()
{
return new List<SelectListItem>{ new SelectListItem {
Text = "WIP", Value = "582"
}, new SelectListItem {
Text = "0888", Value = "595"
} , new SelectListItem {
Text = "0", Value = "0919"
}, new SelectListItem {
Text = "1", Value = "1"
}};
}
public class BinModel
{
public string BinId { get; set; }
}
public ActionResult Index()
{
//load up your select list into the view bag
ViewBag.BinOptions = ViewHelper.PopulateBinList();
//preset the value of your desired object
var bm = new BinModel { BinId = "582" };
return View(bm);
}
@Html.DropDownListFor(x => x.BinId, (IEnumerable<SelectListItem>)ViewBag.BinOptions, null, new { @class ="form-control" })