要将@Html.DropDownListFor设置为只读并设置默认值,可以使用以下方法:
@Html.DropDownListFor(m => m.SelectedValue, Model.SelectList)
public IActionResult Index()
{
var model = new YourViewModel();
model.SelectList = new SelectList(GetDropdownOptions(), "Value", "Text");
// 设置默认值
model.SelectedValue = "default value";
return View(model);
}
@Html.DropDownListFor(m => m.SelectedValue, Model.SelectList, new { @disabled = "disabled" })
@Html.DropDownListFor(m => m.SelectedValue, Model.SelectList, new { @readonly = "readonly" })
[HttpPost]
public IActionResult Index(YourViewModel model)
{
// 通过model.SelectedValue获取选定的值
// 其他逻辑
return RedirectToAction("Index");
}
这样,你就可以将@Html.DropDownListFor设置为只读并设置默认值了。