[英]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.