MVC ModelBind列表框随着Multple选择 [英] MVC ModelBind ListBox With Multple Selections

查看:74
本文介绍了MVC ModelBind列表框随着Multple选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何提交列表框选择到ModelBinder的?

How do I submit the ListBox selections to the ModelBinder?

<%=Html.Hidden("response.Index",index)%>
<%=Html.ListBox("response[index].ChoiceID",
                new MultiSelectList(gc.choice,"ChoiceID","ChoiceText") )%>

gc.choice'是一个List

'gc.choice' is a List

我可以选择的值到模型中的最前一页,而不是第二选择presumably因为我不能更改索引。

I can get the fisrt selected value to the model, but not the second selection presumably because I cannot change the index.

推荐答案

我在一个稍微不同的方式解决了这个...

I have solved this in a slightly different way...

[Model]
public IEnumerable<string> SelectedStores { get; set; }

[View]
<%= Html.ListBox("SelectedStores", 
    (MultiSelectList)ViewData["Stores"], 
    new { size = "8" }) %>

[Controller]
ViewData["Stores"] = 
    new MultiSelectList(StoreItems, "Value", "Text", model.SelectedStores);

因此​​,该模型具有一个IEnumberable将与用户选择来填充。该视图显示与MultiSelectList列表框和控制器的SelectedStores从当它构建了MultiSelectList模型通过了。

So the model has an IEnumberable that will be populated with the user-selections. The view displays the ListBox with the MultiSelectList and the controller passes in the SelectedStores from the model when it constructs the MultiSelectList.

这篇关于MVC ModelBind列表框随着Multple选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆