C# Mvc3和Jquery Multiselect,向服务器发送值不工作?

C# Mvc3和Jquery Multiselect,向服务器发送值不工作?,c#,jquery,asp.net-mvc-3,drop-down-menu,multi-select,C#,Jquery,Asp.net Mvc 3,Drop Down Menu,Multi Select,我正在尝试使用jquerymultiselect插件from和MVC3一起向服务器发送值。如from Darin所示,关键是创建MultiSelectModelBinder类,我猜,该类将识别从客户端发送的值,因为multiselect插件使用[]符号将所选值发送到服务器。我的方法有点不同,我从我的控制器而不是模型中填写dropDownList,保持模型干净,并且能够从数据库中填写列表。我使用Darins示例创建了MultiSelectModelBinder,并将其注册到Application\

我正在尝试使用jquerymultiselect插件from和MVC3一起向服务器发送值。如from Darin所示,关键是创建
MultiSelectModelBinder
类,我猜,该类将识别从客户端发送的值,因为multiselect插件使用[]符号将所选值发送到服务器。我的方法有点不同,我从我的控制器而不是模型中填写dropDownList,保持模型干净,并且能够从数据库中填写列表。我使用Darins示例创建了
MultiSelectModelBinder
,并将其注册到
Application\u Start()中的模型绑定器中。我的问题是,我总是将空模型返回给我的控制器,下面是代码:

型号:

public class PersonsSearchModel
{
    public string Person { get; set; }
    public string Company { get; set; }

    //here is my Cities collection
    public IEnumerable<string> Cities { get; set; }
} 
@model MyNamespace.Model.PersonsSearchModel
@using (Ajax.BeginForm("Search", "Persons", new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "results",
    LoadingElementId = "progress"
},
    new { @id = "searchFormPerson" }
))
{    
  <span>
      @Html.TextBoxFor(x => Model.Person, new { @class = "halfWidth"})
  </span> 
  <span>
      @Html.TextBoxFor(x => Model.Company, new { @class = "halfWidth"})
  </span> 
  <span>
      @Html.ListBoxFor(x => x.Cities, Model.Items, new { @id="combobox1"})
  </span>
  <input name="Search" type="submit" class="searchSubmit" value="submit" />
}
public ActionResult Index()
{

    var listCities = new List<SelectListItem>();
    listCities.Add(new SelectListItem() { Text = "Select one...", Value = "" });
    listCities.Add(new SelectListItem() { Text = "New York", Value = "New York" });
    listCities.Add(new SelectListItem() { Text = "Boston", Value = "Boston" });
    listCities.Add(new SelectListItem() { Text = "Miami", Value = "Miami" });
    listCities.Add(new SelectListItem() { Text = "London", Value = "London" });

    ViewBag.Cities = listCities;

    return View();

}

public class MultiSelectModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var model = (PersonsSearchModel)base.BindModel(controllerContext, bindingContext);
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[]");
        if (value != null)
        {
            return value.RawValue;
        }
        return model;

    }
}
GLOBAL.asax.cs

 protected void Application_Start()
 {
     //...
    //model binder

     ModelBinders.Binders.Add(typeof(IEnumerable<string>), new
     FinessenceWeb.Controllers.PersonsController.MultiSelectModelBinder());
 }

嗯。。。在研究了DOM和Jquery插件之后,发现插件给出了select元素、atribute名称、当前id,所以它们是相同的,表单也是一样的。。看看这个名字,attr。因此,解决方案应该是:

$("#Cities").multiSelect();

干杯

我也遇到了同样的问题,尽管您提供的解决方案仍然有效。还有另一种方法可以省力地完成

实际上,如果您可以将输入参数更改为
List
,并将行
@Html.DropDownListFor
更改为
@Html.ListBoxFor.

这两个控件之间的关键区别在于,第一个控件是单个选择框,第二个控件是多个选择器

希望这能帮助有同样问题的人

$("#combobox1").multiSelect();
$("#Cities").multiSelect();