未设置选定的DropDownList

时间:2016-08-12 13:47:48

标签: html asp.net asp.net-mvc drop-down-menu

我有一个视图,我使用DropDownListFor:

@Html.DropDownListFor(m => m.Nationality, NationalityDD, new { style = "width:185px;" }) 

SelectListItem List遵循以下方式:

List<SelectListItem> NationalityDD = new List<SelectListItem>();
var n0 = new SelectListItem { Value = "", Text = "" };
var n1 = new SelectListItem { Value = "p", Text = @Resources.Localization.nationality_LR_citizen };
var n2 = new SelectListItem { Value = "np", Text = @Resources.Localization.nationality_LR_resident };
var n3 = new SelectListItem { Value = "es", Text = @Resources.Localization.nationality_ES_citizen };
var n4 = new SelectListItem { Value = "ne", Text = @Resources.Localization.nationality_other };

NationalityDD.Add(n0); NationalityDD.Add(n1); NationalityDD.Add(n2); NationalityDD.Add(n3); NationalityDD.Add(n4); 

所以,问题在于当我将np / es / ne值设置到数据库中时,刷新页面后一切正常并且仍然​​显示选定的值,但如果我在页面刷新后设置p则没有选择的值&#34;&#34;。

当我选择p(刷新后)时,DDL会生成此html:

<select id="Nationality" name="Nationality" style="width:185px;">
<option value=""></option>
<option value="p">LR pilsonis</option>
<option value="np">LR Pastāvīgais iedzīvotājs</option>
<option value="es">ES pilsonis</option>
<option value="ne">ne-ES pilsonis</option>
</select> 

当我选择除p之外的某些内容(刷新后):

<select id="Nationality" name="Nationality" style="width:185px;"><option value=""></option>
<option value="p">LR pilsonis</option>
<option value="np">LR Pastāvīgais iedzīvotājs</option>
<option selected="selected" value="es">ES pilsonis</option>
<option value="ne">ne-ES pilsonis</option>
</select> 

DDL出现这种奇怪行为的原因是什么?

1 个答案:

答案 0 :(得分:0)

错误太愚蠢..在数据库中我使用的是族字段创建的不是nvarchar(2),而是简单的char(2),所以在nationality_LR_citizen = p的情况下,它是剩下的空间,所以{{ 1}} == p为false,DDLF未选择此值。