pass all options of <select> from View to controller
2011-11-24 16:04
501 查看
前几天做了点简单的.net MVC相关的东西。有个需求是是把页面上一个列表的所有选项都传回给controller. 多选框的内容,在页面可以删除和添加。初始代码如下:
Index.cshtml:
对于(1),我希望用多选框,单选框不合适;对于(2),多次尝试,却偶尔也能取到,很奇怪。
继续调试,发现原因是:select向controller返回数据时,其实仅仅返回列表中被选中的项! 对于单选列表来说,它总把目前显示的选项当作选中项,自然总可以返回有效值。而对于多选列表,这里我只将它应用于增项和删项,忽视了选择项,导致列表认为无一被选中,故而没有返回值。
解决办法:在Index.cshtml的提交按钮中,加一个响应函数,在按钮被摁下时,先把列表中所有选项置为选中,然后提交整个页面后就可以取到所有option了。
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
相关文章推荐
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- Attempt to present <UINavigationController: 0x7fdf1b100e00> on <ViewController:0x....>
- Pushing a Geneic List of <T> to View state
- Assigning to ‘id<UIWebViewDelegate>’ from incompatible type 'xxxViewController *const __strong'
- Select n numbers from 1 to m with all of array 'all' and none of array 'none'
- Sending 'ViewController *const __strong' to parameter of incompatible type 'id<***Delegate>'
- failed to obtain a cell from its dataSource (<CooperateViewController: 0x7fe6fac7aa20>)'
- Warning: Attempt to present <NextViewController: 0x10966fed0> on
- Passing a list of objects from view to controller via jquery ajax(Json(aspx) -> object(C#))
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x13f6840e0>
- Unbalanced calls to begin/end appearance transitions for <IDOOrderHomeViewController: 0x8da2960>.
- There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key ''.
- Sending 'ViewController *const __strong' to parameter of incompatible type 'id<***Delegate>'
- Sending 'ViewController' to Parameter of Incompatible Type 'id<xxxDelegate>'
- Assigning to 'id<UITextViewDelegate>' from incompatible type 'HBComposeViewController *const __stron
- iOS BUG: Unbalanced calls to begin/end appearance transitions for <XXXViewController: 0x7fcea3730650>.
- ASSIGN COMPONENT <comp> OF STRUCTURE <s> TO <FS>.
- <<Beginning XML with DOM and Ajax From Novice to Professiona>>Contents
- IOS Assigning to 'id<UITabBarControllerDelegate>' from incompatible type...