我會使用強類型的輔助和jQuery。就像這樣:
public class MyViewModel
{
public string Status { get; set; }
public IEnumerable<SelectListItem> Statuses
{
get
{
return new[]
{
new SelectListItem { Value = "online", Text = "Online" },
new SelectListItem { Value = "offline", Text = "Offline" },
new SelectListItem { Value = "all", Text = "All" },
};
}
}
}
然後控制器:
public class HomeController: Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
public ActionResult Status(string status)
{
UserModel model = new UserModel(status);
return View("Users", model);
}
}
最後的觀點:
@model AppName.Models.MyViewModel
@using (Html.BeginForm("Status", "Home", FormMethod.Post, new { id = "myForm" }))
{
@Html.DropDownListFor(
x => x.Status,
new SelectList(Model.Statuses, "Value", "Text"),
new { id = "status" }
)
}
和一些腳本附加到change事件,並提交形式:
<script type="text/javascript">
$(function() {
$('#status').change(function() {
$('myForm').trigger('submit');
});
});
</script>