如何将多选下拉列表绑定到asp.net控制器操作参数
我有一个 html 表单,可以发布到 asp.net-mvc 控制器操作,并且之前工作正常。我刚刚添加了一个新的多选下拉列表(使用 fcbkcomplete jquery 插件),并且我在将其绑定到我刚刚添加的绑定对象的新属性时遇到问题,
我只是列出:
<select id="SponsorIds" name="SponsorIds"></select>
在 html 中,但它看起来像 fcbkcomplete 不知何故将其更改为名称=“SponsorIds[]”。
这是我在浏览器中显示“选定源”后得到的html。
<select multiple="multiple" style="display: none;" id="SponsorIds" name="SponsorIds[]">
这是从插件中吐出的所有 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>
,这是我的控制器操作:
public ActionResult UpdateMe(ProjectViewModel entity)
{
}
视图模型 ProjectViewModel 有一个属性:
public int[] SponsorIds { get; set; }
我认为它可以很好地绑定到此,但似乎并没有,因为它只是显示为“ null”在服务器端。有人能看出这里有什么问题吗?
i have a html form which posts to a asp.net-mvc controller action and previously worked fine. 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
i am just listing:
<select id="SponsorIds" name="SponsorIds"></select>
in the html but it looks like fcbkcomplete somehow changes this to name="SponsorIds[]".
This is the html i get after showing "Selected Source" in the browser.
<select multiple="multiple" style="display: none;" id="SponsorIds" name="SponsorIds[]">
here is all of the html that gets spit out from the plugin
<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:
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. Can anyone see anything wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确命名的列表框(就默认 ASP.NET MVC 模型绑定器可以处理的内容而言)将是:
而不是:
至少如果您希望使用以下命令将其绑定回
int[]
默认模型活页夹。这就是Html.ListBoxFor
帮助器生成的内容。示例:发出:
并且模型绑定器很高兴。
更新:
您还可以有一个能够解析此内容的自定义模型绑定程序:
然后在
Application_Start
中注册此绑定程序:A correctly named list box (in terms of what the default ASP.NET MVC model binder can cope with) would be:
and not:
at least if you expect to bind this back to
int[]
with the default model binder. And that's what theHtml.ListBoxFor
helper generates. Example:emits:
and the model binder is happy.
UPDATE:
You could also have a custom model binder capable of parsing this:
and then register this binder in
Application_Start
: