pass all options of <select> from View to controller
来源:互联网 发布:热力学分析软件ipeak 编辑:程序博客网 时间:2024/05/17 06:30
前几天做了点简单的.net MVC相关的东西。有个需求是是把页面上一个列表的所有选项都传回给controller. 多选框的内容,在页面可以删除和添加。初始代码如下:
Index.cshtml:
<div> <select name="SelClubs" multiple="multiple" style="width:200px"> @foreach (string c in clubs) { <option value="@c">@c</option> } </select> <button type="button" id="deleteclub" onclick="dc(this.form)">delete</button> </div> <div> <input type="text" name="newclub" style="width:200px"/> <button type="button" id='addclub' onclick="ac(this.form)">Add</button> </div> <p> <button type="submit">update</button> </p>homecontroller.cs:
public ActionResult Index(LeagueModel lModel) { var clubs = this.Request.Params["SelClubs"]; if (null != clubs) { string clubStr = clubs as string; var clubList = clubStr.Split(',').ToList() as List<string>; } ... return AnotherView(); }但是我始终无法取到“SelClubs”这个select的值,总是空的。多次调试,发现 (1)如果把select的属性multiple和size去掉,即该列表每次是单选,则controller中可以取到它的值。(2)如果继续使用multiple或者size属性,总是取不到。。
对于(1),我希望用多选框,单选框不合适;对于(2),多次尝试,却偶尔也能取到,很奇怪。
继续调试,发现原因是:select向controller返回数据时,其实仅仅返回列表中被选中的项! 对于单选列表来说,它总把目前显示的选项当作选中项,自然总可以返回有效值。而对于多选列表,这里我只将它应用于增项和删项,忽视了选择项,导致列表认为无一被选中,故而没有返回值。
解决办法:在Index.cshtml的提交按钮中,加一个响应函数,在按钮被摁下时,先把列表中所有选项置为选中,然后提交整个页面后就可以取到所有option了。
<script type="text/javascript">function SelectAll(sel) { for (var i = sel.length - 1; i >= 0; i--) { sel.options[i].selected = true; }}</script>
<p> <button type="submit" onclick="SelectAll(this.form.SelClubs)">update</button> </p>done
- pass all options of <select> from View to controller
- How to pass List from Controller to View in MVC 3.
- Select n numbers from 1 to m with all of array 'all' and none of array 'none'
- Passing a list of objects from view to controller via jquery ajax(Json(aspx) -> object(C#))
- How to pass QEMU command-line options through libvirt
- How to pass QEMU command-line options through libvirt
- MVC - Pass array from model to JS
- Warning: Attempt to dismiss from view controller <LoginViewController: 0xbb9c0f0> while a presentat
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x13f6840e0>
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- How to pass value from Child window to Parent Window
- Applications are expected to have a root view controller at the end of application launch
- Applications are expected to have a root view controller at the end of application解决办法
- Applications are expected to have a root view controller at the end of application launch
- Application windows are expected to have a root view controller at the end of application launch
- Applications are expected to have a root view controller at the end of application launch
- Application windows are expected to have a root view controller at the end of appliaction launch.
- A题
- 在Mac OS X Lion下让Eclipse支持全屏模式的方法
- 批评与自我批评
- 我的数据库2
- 关于烙饼排序问题的优化
- pass all options of <select> from View to controller
- 给MAC字典应用中加入中文词典
- PreferenceActivity 全接触
- 26个ASP.NET常用性能优化方法
- 【二分图+最大独立集】北大 poj 2771 Guardian of Decency
- 修改ARM开发板IP地址(备忘)
- 中关村软件园附近美食一览
- mysql - 产品数量多的时候, 如何通过mysql的slow query log来处置
- Vertex Shader - 合成光(包含环境、散射、镜面)