C# 选中时未正确注册的复选框
复选框非常经典的用法,如果我在字典里写的是if,where,not。当启动网站时,我会进入带有复选框的页面。此处,my dictionnary存在,并用设置为false的值填充: 在测试之前,构建ViewModel: 然后,在检查框并验证后,我返回控制器,但措辞是空的: 测验后,检查复选框并更新值: RadioButton工作正常,所以这真的很奇怪 代码如下: 测验前:C# 选中时未正确注册的复选框,c#,asp.net-mvc,razor,checkboxfor,C#,Asp.net Mvc,Razor,Checkboxfor,复选框非常经典的用法,如果我在字典里写的是if,where,not。当启动网站时,我会进入带有复选框的页面。此处,my dictionnary存在,并用设置为false的值填充: 在测试之前,构建ViewModel: 然后,在检查框并验证后,我返回控制器,但措辞是空的: 测验后,检查复选框并更新值: RadioButton工作正常,所以这真的很奇怪 代码如下: 测验前: public ActionResult MovetoQuiz(string QuizzField, string Quiz
public ActionResult MovetoQuiz(string QuizzField, string QuizzDifficulty)
{
//...//
QuizzViewModel quizzModel = new QuizzViewModel(displayedQuestions, ResultType.Training);
return View("Quizz", quizzModel);
}
测验页面:
@model QRefTrain3.ViewModel.QuizzViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Quizz</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/Site.css")
</head>
<body>
<div>
<!-- For each Question, display a new div with the Title, the question code, the question text, the video if there is one, then the possible answers depending on the type of answers-->
@using (Html.BeginForm("QuizzResult", "Home"))
{
@Html.HiddenFor(m => Model.ResultType)
for (int i = 0; i < Model.DisplayedQuestions.Count; i++)
{
<div class="QuizzQuestion">
<div class="QuizzQuestionTitle">@Model.DisplayedQuestions[i].Id : @Model.DisplayedQuestions[i].Name</div>
<div class="QuizzQuestiontext">@Model.DisplayedQuestions[i].QuestionText</div>
@if (@Model.DisplayedQuestions[i].IsVideo)
{
<div class="QuizzQuestionVideoContainer">
<iframe class="QuizzQuestionVideo" id="ytplayer" type="text/html"
src="@Model.DisplayedQuestions[i].VideoURL"
frameborder="0"></iframe>
</div>
}
@if (@Model.DisplayedQuestions[i].AnswerType == QRefTrain3.Models.AnswerType.SingleAnswer)
{
<div class="RadioButtonAnswers">
@for (int j = 0; j < Model.DisplayedQuestions[i].Answers.Count; j++)
{
@Model.DisplayedQuestions[i].Answers[j].AnswerText
@Html.RadioButtonFor(m => m.DisplayedQuestions[i].AnswersRadio, Model.DisplayedQuestions[i].Answers[j].Id)
}
</div>
}
else if (@Model.DisplayedQuestions[i].AnswerType == QRefTrain3.Models.AnswerType.MultipleAnswer)
{
<div class="RadioButtonAnswers">
@for (int j = 0; j < Model.DisplayedQuestions[i].Answers.Count; j++)
{
@Model.DisplayedQuestions[i].Answers[j].AnswerText
// HERE IS THE CHECKBOXFOR
@Html.CheckBoxFor(m => m.DisplayedQuestions[i].AnswerCheckbox[Model.DisplayedQuestions[i].Answers[j].Id])
}
</div>
}
</div>
}
<input type="submit" value="Validate Answers" />
}
</div>
</body>
</html>
@model QRefTrain3.ViewModel.QuizzViewModel
@{
布局=空;
}
测验
@style.Render(“~/Content/css”)
@style.Render(“~/Content/Site.css”)
@使用(Html.BeginForm(“QuizzResult”、“Home”))
{
@Html.HiddenFor(m=>Model.ResultType)
对于(int i=0;im.DisplayedQuestions[i].AnswersRadio,Model.DisplayedQuestions[i].Answers[j].Id)
}
}
else if(@Model.DisplayedQuestions[i].AnswerType==QRefTrain3.Models.AnswerType.MultipleAnswer)
{
@对于(int j=0;jm.DisplayedQuestions[i].AnswerCheckbox[Model.DisplayedQuestions[i].Answers[j].Id])
}
}
}
}
还有我使用的viewModel(我不得不做一些奇怪的事情,因为radioButtonFor和CheckBoxFor之间不兼容,不要介意):
public class QuestionQuizzViewModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共bool IsVideo{get;set;}
公共字符串VideoURL{get;set;}
公共字符串QuestionText{get;set;}
公共应答类型应答类型{get;set;}
公共列表答案{get;set;}
//包含用户答案的列表。它们必须分开,因为RadioButtonOn和CheckBoxFor在一起使用时不兼容
//使用此选项存储复选框答案:布尔开关。键是答案的ID,值为真/假(选中或未选中)
公共字典应答复选框{get;set;}
//使用此选项存储单选按钮答案:所有单选按钮注册到同一列表,因此属于同一组
//键是问题的ID,值是所选答案的ID
公共列表应答器radio{get;set;}
公共问题quizzviewmodel(int-id、字符串名称、bool-isVideo、字符串videoURL、字符串questionText、AnswerType-AnswerType、列表答案)
{
//...//
//初始化viewModel中使用的列表,具体取决于答案类型。将不使用另一个
if(answerType==answerType.SingleAnswer)
{
AnswersRadio=新列表();
}
else if(answerType==answerType.MultipleAnswer)
{
AnswerCheckbox=新字典();
foreach(答案中的答案问答视图模型a)
{
应答复选框。添加(a.Id,false);
}
}
如您所见,我初始化了将用于该问题的列表,然后将假值添加到AnswerCheckbox词汇表中。我希望CheckBoxFor更改这些值,并将其返回到POST Controller中。C#.NET MVC在CheckBoxFor()命令中存在一些问题。CheckBoxFor()自动生成具有相同名称的隐藏字段&如果该字段未单独在[Div]中隔离,它将偶尔删除发送到服务器的检查。即使在[display field]Div中包含标题或验证,也可能导致CheckBoxFor()出现问题。解决方法是隔离CheckBoxFor()或添加[onchange]更新&它似乎更加一致地工作
<div class="display-field">
@Html.CheckBoxFor(m => Model.DisplayedQuestions[i].AnswerCheckbox[j],
new { onchange = "this.value = this.checked;"})
</div>
@(m=>Model.DisplayedQuestions[i].AnswerCheckbox[j],
新建{onchange=“this.value=this.checked;”})
或者,您可以执行以下操作&在没有隐藏字段的情况下获得相同的结果
<input type="checkbox" id="AnswerCheckbox" name="AnswerCheckbox"
@if (Model.DisplayedQuestions[i].AnswerCheckbox[j])
{ @Html.Raw("checked value=\"true\"") ; }
@if (!Model.DisplayedQuestions[i].AnswerCheckbox[j])
{ @Html.Raw("value=\"false\"") ; }
onchange="this.value = this.checked;" />
或者简而言之
<input type="checkbox" id="AnswerCheckbox" name="AnswerCheckbox"
@(Model.DisplayedQuestions[i].AnswerCheckbox[j] ?
"checked value=\"true\"" : "value=\"false\"")
onchange="this.value = this.checked;" />
大写模型引用来自控制器的数据。请确保将该模型作为ActionResult或IActionResult函数的参数包含在控制器中
注意:问题很可能与生成的隐藏字段有关,该字段与生成的CheckBoxFor()同名复选框字段,bc该框并不总是使用标准HTML5调用进行更新,但它们每次都使用标准输入布局工作。不要使用
字典和应答复选框和应答无线电将为空
,因为if
块意味着您生成了非零基/非零基utve系列
<input type="checkbox" id="AnswerCheckbox" name="AnswerCheckbox"
@(Model.DisplayedQuestions[i].AnswerCheckbox[j] ?
"checked value=\"true\"" : "value=\"false\"")
onchange="this.value = this.checked;" />