C# 如何在.net core中验证动态控件
我正在创建调查应用程序,我正在为其动态创建控件(如复选框、单选按钮、文本框等) 根据分配给问题的控件类型,每个问题将具有控件,并根据问题类型呈现答案选项(复选框,单选按钮) 在下一次/上一次导航中,我将当前页面的答案存储在数据库中。在浏览页面时,我正在进行ajax调用以保存数据库,而我的UI/控件不在表单中 我已经根据我的LMS_调查问题和LMS_调查问题选项选择表创建了ViewModel 因此,在for循环中创建view中的UI时,我在创建AnswerChoice控件时直接将C# 如何在.net core中验证动态控件,c#,jquery,asp.net-mvc,asp.net-core-mvc,asp.net-core-1.0,C#,Jquery,Asp.net Mvc,Asp.net Core Mvc,Asp.net Core 1.0,我正在创建调查应用程序,我正在为其动态创建控件(如复选框、单选按钮、文本框等) 根据分配给问题的控件类型,每个问题将具有控件,并根据问题类型呈现答案选项(复选框,单选按钮) 在下一次/上一次导航中,我将当前页面的答案存储在数据库中。在浏览页面时,我正在进行ajax调用以保存数据库,而我的UI/控件不在表单中 我已经根据我的LMS_调查问题和LMS_调查问题选项选择表创建了ViewModel 因此,在for循环中创建view中的UI时,我在创建AnswerChoice控件时直接将SurveyQue
SurveyQuestionOptionChoiceID
指定为控件ID,并将其存储在SurveyUserAnswer表中
模型
因此,我的问题是如何在这种情况下验证动态创建的控件?您可以对控件上的数据属性使用不引人注目的jQuery验证。
阅读更多相关信息。是的,但我无法在模型属性上添加必需的属性,因为有时不需要验证。因此,如果需要验证,则应将其添加到控件中,否则您可以在动态添加的输入上添加数据必需属性,如链接中所述。不,这不起作用!我的控件处于引导模式,这有问题吗?@Dawid Rutkowski感谢您的编辑。我的问题有什么解决办法吗?
public class LMS_TraineeSurveyPaginationViewModel
{
public List<LMS_SurveyQuestions> SurveyQuestions { get; set; }
public List<LMS_SurveyQuestionOptionChoice> SurveyQuestionOptionChoice { get; set; }
public SurveyPager Pager { get; set; }
}
@foreach (var item in Model.SurveyQuestions)
{
foreach (var data in Model.SurveyQuestionOptionChoice.Where(x => x.SurveyQuestionID == item.SurveyQuestionID).ToList())
{
if (item.QuestionTypeID == QuestionType.RadioButton)
{
<li style="list-style:none;">
<input type="radio" name="rb" id="@data.SurveyQuestionOptionChoiceID" />
<span>@data.OptionChoice</span>
</li>
}
else if (item.QuestionTypeID == QuestionType.CheckBox)
{
<li style="list-style:none;">
<input type="checkbox" id="@data.SurveyQuestionOptionChoiceID" name="@data.SurveyQuestionOptionChoiceID" " />
<span>@data.OptionChoice</span>
</li>
}
}
}
function SaveValues() {
var surveyQuestion = @Html.Raw(Json.Serialize(Model.SurveyQuestions.ToArray()));
var surveyQuestionOptionChoide = @Html.Raw(Json.Serialize(Model.SurveyQuestionOptionChoice.ToArray()));
for (item in surveyQuestion) {
var surveyQuestionID=surveyQuestionViewModel[item].SurveyQuestionID;
var filteredData = surveyQuestionOptionChoide.filter(function(filteredItem) {
return (filteredItem.SurveyQuestionID==surveyQuestionID);
});
for (optionChoice in filteredData) {
if(surveyQuestion[item].QuestionTypeID=='@QuestionType.RadioButton') {
if (($('#'+SurveyQuestionOptionChoiceID).prop("checked"))) {
surveyUserAnswer.push({ SurveyUserAnswerID: filteredData[optionChoice].SurveyUserAnswerID==null?0:filteredData[optionChoice].SurveyUserAnswerID,
SurveyQuestionOptionChoiceID: SurveyQuestionOptionChoiceID,SurveyUserID:'@ViewBag.SurveyUserID',AnswerText:null,
MarksObtained:filteredData[optionChoice].Marks,WhenCreated:'@DateTime.UtcNow',WhoCreated:'@tenant.UserID'});
}
}
}
}
$.post('@Url.Action("GetTraineeSurvey", "Survey")', {SurveyID:surveyID,page:page, surveyUserAnswer: surveyUserAnswer,PrevBranchQuestionPage:currentPage,IsBranchQuestionAvailable:IsBranchQuestionAvailable }, function (data) {
$('#surveyModalContent').html('');
$('#surveyModalContent').html(data);
$("#surveyModal").modal('show');
}).fail(function() {
alert( "error in GetTraineeSurvey" );
}).success(function() { });
}