提问人:Jay 提问时间:11/9/2023 更新时间:11/10/2023 访问量:68
Asp.net Core MVC 6 - bootstrap modal bind bool?复选框
Asp.net Core MVC 6 - bootstrap modal bind bool? checkbox
问:
我在布尔值在部分模态引导视图上保持其值时遇到了问题。
在模型中有一个布尔属性
[Required]
public bool? IsActive { get; set; }
在索引控制器中,我有几个正在生成的 Viewbag 列表。ViewBag.IsActive 将生成列表,但在加载部分视图时未选择正确的值。列表的其余部分正在工作,并且在单击编辑按钮时选择了正确的值。
ViewBag.IsActive = new List<SelectListItem>
{
new SelectListItem {Text = "--Not Set--", Value = ""},
new SelectListItem {Text = "True", Value=true.ToString()},
new SelectListItem {Text = "False", Value=false.ToString()}
};
ViewBag.ExamType = new List<SelectListItem>
{
new SelectListItem{ Text="Credentialed", Value = "C" },
new SelectListItem{ Text="Review", Value = "R" },
new SelectListItem{ Text="Scantron", Value = "S" },
new SelectListItem{ Text="Practice", Value = "P" }
};
ViewBag.ExamTime = new List<SelectListItem>
{
new SelectListItem{ Text="1 Hour", Value = "3600" },
new SelectListItem{ Text="1.25 Hour", Value = "4500" },
new SelectListItem{ Text="1.5 Hours", Value = "5400" },
new SelectListItem{ Text="1.75 Hours", Value = "6300" },
new SelectListItem{ Text="2 Hours", Value = "7200" },
new SelectListItem{ Text="2.5 Hours", Value = "9000" },
new SelectListItem{ Text="3 Hours", Value = "10800" },
new SelectListItem{ Text="4.5 Hours", Value = "16200" },
new SelectListItem{ Text="No Time Limit", Value = "0" }
};
ViewBag.CredentialId = new SelectList(db.Credentials.Where(x => x.IsActive == true || x.CredentialCode == "5YEAR").OrderBy(x => x.CredentialDesc), "CredentialId", "CredentialDesc");
ViewBag.CredentialStatusId = new SelectList(db.CredentialStatuses.OrderBy(x => x.CredentialStatusDescription), "CredentialStatusId", "CredentialStatusDescription");
在“索引”视图中,有一个函数更新下面的值,我有 val.(data.IsActive) 用于 ddlActiveStatusOption,IsActive 用于测试尝试并返回值。
function FunctionEdit(element) {
var id = $(element).closest('tr').find('#hdncode').val();
CreateNew();
$.ajax({
type: 'post',
url: '@Url.Action("GetById", "Exams")/' + id,
data: { code: id },
async: false,
success(data) {
console.log("what is this",data)
console.log("isActive", data.isActive)
if (data != null) {
$('#CredentialId').val(data.credentialId);
$('#CredentialStatusId').val(data.credentialStatusId);
$('#ExamTime').val(data.examTime);
$('#ExamType').val(data.examType);
$('#IsActive').val(data.isActive);
$('#ddlActiveStatusOption').val(data.isActive)
}
},
error() {
}
});
}
The GetById
public Exam GetById(int Id)
{
var exam = db.Exams.FirstOrDefault(x => x.ExamId == Id);
return exam;
}
然后调用局部视图
<div class="form-group">
<label asp-for="CredentialId" class="control-label"></label>
<select asp-for="CredentialId" class="form-control" asp-items="ViewBag.CredentialId"></select>
<span asp-validation-for="CredentialId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CredentialStatusId" class="control-label"></label>
<select asp-for="CredentialStatusId" class="form-control" asp-items="ViewBag.CredentialStatusId"></select>
<span asp-validation-for="CredentialStatusId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ExamTime" class="control-label"></label>
<select asp-for="ExamTime" class="form-control" asp-items="ViewBag.ExamTime"></select>
<span asp-validation-for="ExamTime" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ExamType" class="control-label"></label>
<select asp-for="ExamType" class="form-control" asp-items="ViewBag.ExamType"></select>
<span asp-validation-for="ExamType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="IsActive" class="control-label"></label>
<select asp-for="IsActive" class="form-control" asp-items="ViewBag.IsActive"></select>
<span asp-validation-for="IsActive" class="text-danger"></span>
<input id="ddlActiveStatusOption" class="form-control" />
如果我将 id 放在输入标记中,则可以获取 ddlActiveStatusOption 的 true 或 false 值,但是不会为 IsActive 保留任何值。我做错了什么?我已经阅读了以下链接的文档,并尝试了许多答案。
答:
评论