Technical information using dropdown values to pass to controller
I am having difficulty in doing the fol开发者_开发知识库lowing and if anyone can give me an idea how I can achieve the following:
Upon a new selection in the dropdown, I call the action Status and passing the selected value in the dropdown as parameter.
I have a dropdown as follows in my view
@Html.DropDownList("Status", Model.DisplayStatus)
In My controller, I am having
private ActionResult Status(string active = "All")
{
UserModel model = new UserModel(active);
return View("Users", model);
}
I would use strongly typed helpers and jquery. Like this:
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" },
};
}
}
}
then a controller:
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);
}
}
and finally the view:
@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" }
)
}
and some script to attach to the change event and submit the form:
<script type="text/javascript">
$(function() {
$('#status').change(function() {
$('myForm').trigger('submit');
});
});
</script>
精彩评论