在ASP.NET MVC中提交下拉列表,您需要遵循以下步骤:
public class DropDownModel
{
public string SelectedOption { get; set; }
public List<SelectListItem> Options { get; set; }
}
public ActionResult Index()
{
var model = new DropDownModel();
model.Options = new List<SelectListItem>
{
new SelectListItem { Text = "Option 1", Value = "1" },
new SelectListItem { Text = "Option 2", Value = "2" },
new SelectListItem { Text = "Option 3", Value = "3" }
};
return View(model);
}
Html.DropDownListFor
帮助器方法创建下拉列表。@model DropDownModel
@Html.DropDownListFor(m => m.SelectedOption, Model.Options, "Select an option", new { id = "myDropDown" })
@section Scripts {
<script>
$(document).ready(function () {
$("#myDropDown").on("change", function () {
$("form").submit();
});
});
</script>
}
[HttpPost]
public ActionResult Index(DropDownModel model)
{
// Process the selected option
string selectedOption = model.SelectedOption;
// Do something with the selected option
// Redirect or return a view as needed
return RedirectToAction("Index");
}
这样,在用户从下拉列表中选择一个选项并更改时,表单将自动提交并将选定的选项传递给控制器进行处理。
领取专属 10元无门槛券
手把手带您无忧上云