コントロールを動的に作成するサーベイアプリケーションを作成しています(チェックボックス、ラジオボタン、テキストボックスなどのように)。.netコアでダイナミックコントロールを検証する方法
各質問には、質問に割り当てられたコントロールタイプに応じてコントロールがあり、回答タイプ(チェックボックス、ラジオボタン)がレンダリングされます。
次へ/前へナビゲーション私は現在のページの回答をデータベースに保存しています。私はデータベースの保存と私のUI /コントロールのためのajax呼び出しを行っているページをナビゲートしている間はフォームにありません。
LMS_SurveyQuestionsテーブルとLMS_SurveyQuestionOptionChoiceテーブルに基づいてViewModelを作成しました。
したがって、forループのビューでUIを作成しているときに、私はAnswerChoiceコントロールを作成する際にControl IDとしてSurveyQuestionOptionChoiceID
を直接割り当てて、SurveyUserAnswerテーブルのテーブルに保存しました。
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>
}
}
}
をレンダリングし、どのように私はSurveyUserAnswerのモデルとしてJSON/JSの配列を作成し、中にそれを保存しているデータベースに答えを節約しながら、データベースを以下のように作成します。以下はラジオボタンの例です
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() { });
}
私はこのシナリオで動的に作成されたコントロールをどのように検証できますか?
@ Dawid Rutkowski編集をありがとう。私の問題の解決策はありますか? – Rohit