具有预选值的 MVC 下拉列表不允许在需要字段时提交表单

MVC dropdown list with preselected value does not allow submitting the form when the field is required

我有一个由各州填充的下拉列表。当加载更新地址页面时,该字段已经填写并且状态为 selected。我在模型中的这个 State 字段中添加了数据注释 "Required",但它不允许预设值。它没有显示消息,但我无法提交表单,我需要 select 状态才能提交表单(我猜是重新 selecting)。如何告诉表单预selected 值是要验证的值?

[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "StateRequired")]   
public string State { get; set; }
@Html.DropDownListFor(m => m.State, (IEnumerable<SelectListItem>)Model.StatesList, Model.SelectedState, new { @class = "form-control form-select" })

SelectedState是一个模型属性,其中用户地址的Db值已经存储在Db中。

好的,这是解决方案,@LarryBud 评论指出了我正确的方向。

所以基本上,选定的值没有设置在列表中,但是有人在文本 "select a state" 通常所在的位置添加了该值,所以状态显示但不是列表的一部分列表。

我刚刚在状态列表中将用户的状态标记为选中,然后就解决了。

DDList.Add(new SelectListItem() { Text = row["State"].ToString(), Value = row["Abbreviation"].ToString(), Selected = true });