如何设置复选框值以从mvc中的控制器检查

编程入门 行业动态 更新时间:2024-10-28 08:30:35
本文介绍了如何设置复选框值以从mvc中的控制器检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的视图中有一个html表。我有复选框来选择记录并将它们插入我的表格中。这是我的代码:

Hi, I have a html table in my view. I have checkboxes to select record and insert them into my table. here is my code :

<table id="dtsResult" class="table table-striped table-bordered" cellspacing="0" width="100%"> <thead> <tr> <th>SelectAll<input type="checkbox" id="CheckAllRes" /></th> <th>Unit</th> </tr> </thead> <tbody> @foreach (var item in ViewBag.Result) { <tr> <td><input type="checkbox" id=@item.RecordID name="res" class="chkres" /></td> <td> @item.UnitNo</td> </tr> } </tbody> </table>

现在我的问题是在插入记录后我再次打开这个页面,然后我想检查已经插入的所有复选框。任何人都可以帮助我解决为什么以及如何解决这个问题所以我从db检查了这些复选框。请帮忙。 谢谢

Now my problem is after insert record when i open again this page then i want to checked all check boxes which is already inserted. Can anyone help me with why and how to fix this so I checked these check boxes from db. Please help. Thanks

推荐答案

<pre lang="c#">@foreach (var item in ViewBag.Result) {

<tr> <td> <input type="checkbox" @if(item.IsSaved){<text>checked</text>} id="@item.RecordID" name="res" class="chkres" /></td> <td>@item.UnitNo</td> </tr>

}

@foreach (var item in ViewBag.Result) { <tr> <td><input type="checkbox" id="@item.RecordID" name="res" class="chkres" checked="@item.IsChecked" /></td> <td> @item.UnitNo</td> </tr> }

item.IsChecked 必须是bool,而你的 ViewBag.Result 必须包含它。 -KR

item.IsChecked has to be bool, and your ViewBag.Result must contain it. -KR

模式或任务不清楚,因为您没有解释如何找到添加到数据库的记录。我假设您的 item 对象包含一个属性 IsSaved 。 也就是说,复选框输入可以改为这样的状态, The schema or task is unclear, as you have not explained how would you find the records that are added to the database. I would assume that your item object holds a property "IsSaved". That said, the checkbox input can be altered for its state like this, @foreach (var item in ViewBag.Result) { <tr> <td> <input type="checkbox" checked="@item.IsSaved" id="@item.RecordID" name="res" class="chkres" /></td> <td>@item.UnitNo</td> </tr> }

这将检查一个值,无论是真还是假。然后它会检查它,或者不加以检查。这取决于你的对象。以它代表你的系统的方式实现它。

This would now check for a value, either true or false. Then it would either check it, or leave it unchecked. That depends on your object. Implement it in a way that it represents your system.

更多推荐

如何设置复选框值以从mvc中的控制器检查

本文发布于:2023-10-12 01:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   复选框   如何设置   mvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!