提问人:ahmed abdelaziz 提问时间:10/16/2023 最后编辑:ahmed abdelaziz 更新时间:10/16/2023 访问量:44
调用操作时复选框的问题值不影响模型属性speakstuff?
Issue value of checkbox not affect on model property speakstuff when call action?
问:
我在 MVC 应用程序 asp.net 工作。未检测到“我遇到问题”复选框值
将模型属性的值从 true 更改为 false 或反转。speakstuff
所以我需要如果是,则选中 true,如果没有选中,则为 false,然后影响模型属性上的此值。speakstuff
HTML 脚本表示“是”或“否”复选框
<table style="border: 1px solid black;width:100%;margin-bottom:5px;">
<tr>
<td style="width: 50%; font-weight: bold; padding-top: 10px;">
@Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff ? "checked" : "") />
Yes
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") />
No
</div>
</td>
</table>
我通过java脚本从表单访问值
<script type="text/javascript">
function submit() {
var ResignationRequester = new Object();
ResignationRequester.SpeakStuff = document.getElementById("SpeakStuff").value;
}
</script>
jQuery code for single check
$(document).ready(function () {
$('.speak-stuff-checkbox').on('change', function () {
$('.speak-stuff-checkbox').not(this).prop('checked', false);
});
});
using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
{
<a onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px;
margin-left: 1300px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>
}
[HttpPost]
public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
{
}
那么如何解决这个问题呢?
更新的帖子
表示属性 SpeakStuff 的模型
public class ResignationRequester
{
[Display(Name = "Employee No : ")]
[Required]
public int EmpID { get; set; }
[Display(Name = "Request No")]
public int RequestNo { get; set; }
public bool SpeakStuff { get; set; }
}
答:
1赞
Yat Fei Leong
10/16/2023
#1
为 SpeakStuff 创建一个隐藏字段
@Html.HiddenFor(x => Model.SpeakStuff)
删除复选框中的 SpeakSuff 属性
<div class="col-md-7">
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox") />
Yes
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox") />
No
</div>
修改您的 javascript 以在检查选择时将值更新为隐藏字段
$(document).ready(function () {
$('.speak-stuff-checkbox').on('change', function () {
$('.speak-stuff-checkbox').not(this).prop('checked', false);
var res = $(this).val();
$('#Model_SpeakStuff').val($(this).val());
//alert($('#test').val());
});
});
speakstuff 值派生自隐藏字段。
评论