jQuery的多选DROPDOWNLIST如何访问成果? [英] jQuery MultiSelect dropdownlist how to access results?
本文介绍了jQuery的多选DROPDOWNLIST如何访问成果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能得到一个jQuery多选的DropDownList结果每一个MVC3 /剃刀的叫什么?
How can I get the results from a JQUery Multi-select dropdownlist as called per an mvc3/razor?
http://abeautifulsite.net/blog/2008/04/jquery-multiselect /
推荐答案
多选插件使用 []
符号来选定值发送给服务器。与往常一样,我们开始通过编写一个视图模型:
The multiselect plugin uses the []
notation to send the selected values to the server. As always we start by writing a view model:
public class MyViewModel
{
public IEnumerable<string> SelectedValues { get; set; }
public IEnumerable<SelectListItem> Items
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
};
}
}
}
然后控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
相应的视图:
@model MyViewModel
<script src="@Url.Content("~/Scripts/jquery.multiSelect.js")" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#SelectedValues").multiSelect();
});
</script>
@using (Html.BeginForm())
{
@Html.ListBoxFor(x => x.SelectedValues, Model.Items)
<button type="submit">OK</button>
}
和最后用相关的模型绑定的的IEnumerable&LT;串&GT;
键入和将与在 []
由插件使用的符号:
and finally a model binder associated with the IEnumerable<string>
type and which will work with the []
notation used by the plugin:
public class MultiSelectModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = (MyViewModel)base.BindModel(controllerContext, bindingContext);
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[]");
if (value != null)
{
return value.RawValue;
}
return model;
}
}
最后一部分是注册在模型绑定的Application_Start
:
ModelBinders.Binders.Add(typeof(IEnumerable<string>), new MultiSelectModelBinder());
这篇关于jQuery的多选DROPDOWNLIST如何访问成果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文