简体   繁体   English

如何将多选下拉列表绑定到asp.net控制器动作参数

[英]how to bind multiselect dropdown to asp.net controller action parameter

i have a html form which posts to a asp.net-mvc controller action and previously worked fine. 我有一个HTML格式,发布到asp.net-mvc控制器动作,以前工作正常。 i just added a new multiselect dropdown (using the fcbkcomplete jquery plugin) and i am having problems binding it to a new property that i just added of my binding object 我刚刚添加了一个新的多选下拉列表(使用fcbkcomplete jquery插件),我遇到了将它绑定到我刚刚添加了我的绑定对象的新属性的问题

i am just listing: 我刚刚上市:

 <select id="SponsorIds" name="SponsorIds"></select>

in the html but it looks like fcbkcomplete somehow changes this to name="SponsorIds[]". 在HTML中,但看起来fcbkcomplete以某种方式将此更改为name =“SponsorIds []”。

This is the html i get after showing " Selected Source " in the browser. 这是我在浏览器中显示“ Selected Source ”后得到的html。

<select multiple="multiple" style="display: none;" id="SponsorIds" name="SponsorIds[]">

here is all of the html that gets spit out from the plugin 这里是从插件中吐出的所有html

 <select multiple="multiple" style="display: none;" id="SponsorIds" name="SponsorIds[]">
<option class="selected" selected="selected" value="9">MVal</option>
</select>
<ul class="holder">
<li rel="9" class="bit-box">MVal<a href="#" class="closebutton"></a></li>
<li id="SponsorIds_annoninput" class="bit-input"><input size="1" class="maininput"    type="text"></li>
</ul>
<div style="display: none;" class="facebook-auto">
<ul style="width: 512px; display: none; height: auto;" id="SponsorIds_feed">
<li class="auto-focus" rel="9"><em>MVal</li></ul><div style="display: block;" class="default">Type Name . . .
</div>
</div>

and here is my controller action: 这是我的控制器动作:

 public ActionResult UpdateMe(ProjectViewModel entity)
    {
    }

The view model, ProjectViewModel has a property: 视图模型,ProjectViewModel有一个属性:

   public int[] SponsorIds { get; set; }

which i thought would bind fine to this but doesn't seem to as it just shows up as "null" on the serverside. 我认为它会很好地绑定到这个,但似乎并没有,因为它只是在服务器端显示为“null”。 Can anyone see anything wrong here? 谁能在这里看到任何错误?

A correctly named list box (in terms of what the default ASP.NET MVC model binder can cope with) would be: 一个正确命名的列表框(根据默认的ASP.NET MVC模型绑定器可以处理的)将是:

name="SponsorIds"

and not: 并不是:

name="SponsorIds[]"

at least if you expect to bind this back to int[] with the default model binder. 至少如果你希望用默认的模型绑定器将它绑定回int[] And that's what the Html.ListBoxFor helper generates. 这就是Html.ListBoxFor助手生成的内容。 Example: 例:

@Html.ListBoxFor(
    x => x.SponsorIds, 
    new SelectList(
        new[] { 
            new { Value = "1", Text = "MVal1" },
            new { Value = "2", Text = "MVal2" }, 
            new { Value = "3", Text = "MVal3" }, 
        }, 
        "Value", "Text"
    )
)

emits: 发出:

<select id="SponsorIds" multiple="multiple" name="SponsorIds">
    <option value="1">MVal1</option>
    <option value="2">MVal2</option>
    <option value="3">MVal3</option>
</select>

and the model binder is happy. 模型活页夹很高兴。


UPDATE: 更新:

You could also have a custom model binder capable of parsing this: 您还可以使用自定义模型绑定器来解析此问题:

public class FCBKCompleteIntegerArrayModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[]");
        if (values != null && !string.IsNullOrEmpty(values.AttemptedValue))
        {
            // TODO: A minimum of error handling would be nice here
            return values.AttemptedValue.Split(',').Select(x => int.Parse(x)).ToArray();
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}

and then register this binder in Application_Start : 然后在Application_Start注册此绑定器:

protected void Application_Start()
{
    ...
    ModelBinders.Binders.Add(typeof(int[]), new FCBKCompleteIntegerArrayModelBinder());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET MVC 中使用 routeValue 参数调用 controller 操作 - How to call controller action with a routeValue parameter in ASP.NET MVC 如何在 Asp.NET MVC 中使用复选框创建多选下拉菜单 - How to Create MultiSelect dropdown with checkboxes in Asp.NET MVC ASP.NET Dropdown将空值传递给控制器​​操作 - ASP.NET Dropdown passing null value to controller action 如何在ASP.NET MVC上为GET和POST操作绑定Dictionary类型参数 - How to bind Dictionary type parameter for both GET and POST action on ASP.NET MVC ASP.NET MVC 4:如何在控制器操作中获取选定的下拉值? - ASP.NET MVC 4 : How to get selected dropdown values in controller action? 参数未传递给ASP.NET MVC中Controller的操作 - Parameter not being passed to Controller's action in ASP.NET MVC ASP.NET MVC F#控制器操作忽略参数 - ASP.NET MVC F# controller action ignoring parameter ASP.NET MVC SQL查询作为控制器操作方法的参数 - Asp.net mvc sql query as parameter to controller action method ASP.NET MVC,JSON与FormCollection参数一起发布到控制器操作 - ASP.NET MVC, JSON post to controller action with FormCollection parameter asp.net mvc ActionFilterAttribute-重定向到“控制器/动作/参数” - asp.net mvc ActionFilterAttribute - redirect to “controller/action/parameter”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM