在视图中使用Html.DropDownListFor()方法,并将选定项的值与模型属性绑定。以下是示例代码:
在视图文件(.cshtml)中: @model Models.MyViewModel //模型类 ... @Html.DropDownListFor(m => m.SelectedOption, new SelectList(Model.Options, "Value", "Text"))
在模型类中:
public class MyViewModel {
public List
在控制器中:
[HttpGet]
public IActionResult Index() {
var model = new MyViewModel {
Options = new List
[HttpPost] public IActionResult Index(MyViewModel model) { //在此处使用选定项的值(model.SelectedOption)进行操作 return View(model); }