以下是一个使用ASP.NET Core和Razor Pages的示例,演示如何从DropDownList返回值的解决方法:
在Razor Pages中,首先在.cshtml文件中创建一个DropDownList,然后在.cshtml.cs文件中处理选中项的值。
1.在.cshtml文件中,使用元素创建一个DropDownList,并使用
元素添加选项:
2.在.cshtml.cs文件中,定义一个属性来存储选中项的值,并在构造函数中初始化DropDownList的选项:
public class IndexModel : PageModel
{
public List DropDownOptions { get; set; }
public string SelectedValue { get; set; }
public IndexModel()
{
DropDownOptions = new List
{
new SelectListItem { Value = "1", Text = "Option 1" },
new SelectListItem { Value = "2", Text = "Option 2" },
new SelectListItem { Value = "3", Text = "Option 3" }
};
}
public IActionResult OnGet()
{
return Page();
}
public IActionResult OnPost()
{
// 处理选中项的值
// 此处只是简单地将选中项的值存储到SelectedValue属性中,并返回到页面
return Page();
}
}
3.在.cshtml文件中,使用元素将DropDownList包装起来,并在提交表单时调用
OnPost
方法:
这样,当用户选择DropDownList中的选项并提交表单时,OnPost
方法将会被调用,并且选中项的值将存储在SelectedValue
属性中。您可以根据需要在OnPost
方法中进一步处理选中项的值。