在ASP.NET Core中使用DropDownList时,可能会遇到返回Undefined的问题。这是因为DropDownList不会自动绑定到控制器中的Model,它只会返回选定项的值。
为了解决这个问题,可以手动将选定项的值绑定到Model中。以下是一个示例:
在View中,使用DropDownList代码如下:
在Model中,添加SelectedValue属性:
public class MyModel
{
public string SelectedValue { get; set; }
public List DropdownListValues { get; set; }
}
在控制器中,将选定项的值绑定到Model中:
[HttpPost]
public IActionResult MyAction(MyModel model)
{
// The selected value is in model.SelectedValue
// ...
}
通过以上步骤,即可避免ASP.NET Core中DropdownList返回Undefined的问题。