我有一个奇怪的问题,MVC 下拉选择的值未在页面加载上预先选择。
我的模型是:
public class DocumentTypesViewModel
{
[Required(ErrorMessage = "DocumentType is required")]
public int OHDocumentTypeId { get; set; }
public string OHDocumentTypeDescription { get; set; }
}
public class ClientAdvancedSearchViewModel
{
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "DocumentType")]
public string DocumentTypeId { get; set; }
public IEnumerable<SelectListItem> DocumentTypes { get; set; }
}
在我的控制器中,我正在像这样填充 ClientAdvancedSearchViewModel
[HttpGet]
public ActionResult ClientAdvancedSearch()
{
ClientAdvancedSearchViewModel clientAdvancedSearchViewModel = iClientReferralRecordsRepository.GetDocumentMetadata();
//DocumentTypes Dropdown
var ddlDocumentTypes = iDocumentTypeRepository.GetDocumentTypes();
clientAdvancedSearchViewModel.DocumentTypes = new SelectList(ddlDocumentTypes, "OHDocumentTypeId", "OHDocumentTypeDescription",clientAdvancedSearchViewModel.DocumentTypeId);
return View(clientAdvancedSearchViewModel);
}
最后在视图中:
<td>
<div class="editor-label">
@Html.LabelFor(model => model.DocumentTypes)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.DocumentTypeId, Model.DocumentTypes, "Please Select", new { @id = "ddlDocumentType" })
</div>
</td>
我相信下拉列表的名称相同是 x => x.DocumentTypeId,因为我认为,我的值没有预先选择。
这是为下拉菜单生成的 HTML 的 ViewSource
<select id="ddlDocumentType" name="DocumentTypeId">
<option value="">Please Select</option>
<option value="20">records</option>
<option value="21"> record1</option>
..
如何重命名我的下拉列表名称或如何解决我的问题?
谢谢
更新:添加了错过的线路
ClientAdvancedSearchViewModel clientAdvancedSearchViewModel = iClientReferralRecordsRepository.GetDocumentMetadata();