如何设置复选框值以从mvc中的控制器检查 [英] how set checkbox value to checked from controller in 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 youritem
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中的控制器检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!