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

发布于 2024-10-18 14:23:59 字数 1557 浏览 12 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-25 14:23:59

正确命名的列表框(就默认 ASP.NET MVC 模型绑定器可以处理的内容而言)将是:

name="SponsorIds"

而不是:

name="SponsorIds[]"

至少如果您希望使用以下命令将其绑定回 int[]默认模型活页夹。这就是 Html.ListBoxFor 帮助器生成的内容。示例:

@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"
    )
)

发出:

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

并且模型绑定器很高兴。


更新:

您还可以有一个能够解析此内容的自定义模型绑定程序:

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);
    }
}

然后在 Application_Start 中注册此绑定程序:

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

A correctly named list box (in terms of what the default ASP.NET MVC model binder can cope with) would be:

name="SponsorIds"

and not:

name="SponsorIds[]"

at least if you expect to bind this back to int[] with the default model binder. And that's what the Html.ListBoxFor helper generates. 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:

protected void Application_Start()
{
    ...
    ModelBinders.Binders.Add(typeof(int[]), new FCBKCompleteIntegerArrayModelBinder());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文