the questionnaire with list of questions and answers added
This commit is contained in:
parent
f83d5c492b
commit
9938e696f6
10 changed files with 300 additions and 461 deletions
|
|
@ -51,7 +51,7 @@ namespace Data
|
|||
|
||||
|
||||
modelBuilder.Entity<Questionnaire>()
|
||||
.HasKey(q => q.Id);
|
||||
.HasKey(q => q.Id);
|
||||
|
||||
modelBuilder.Entity<Questionnaire>()
|
||||
.HasMany(q => q.Questions)
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ namespace Services.Implemnetation
|
|||
public void Update(Questionnaire questionnaire)
|
||||
{
|
||||
_context.Questionnaires.Update(questionnaire);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
using Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Model;
|
||||
using Services.Implemnetation;
|
||||
|
||||
using Services.Interaces;
|
||||
using System.Security.Cryptography;
|
||||
using Web.ViewModel.QuestionnaireVM;
|
||||
using Web.ViewModel.QuestionVM;
|
||||
|
||||
|
||||
namespace Web.Areas.Admin.Controllers
|
||||
{
|
||||
|
|
@ -135,11 +137,13 @@ namespace Web.Areas.Admin.Controllers
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Edit(int id)
|
||||
public IActionResult Edit(int? id)
|
||||
{
|
||||
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
||||
var questionTypes = Enum.GetValues(typeof(QuestionType))
|
||||
.Cast<QuestionType>()
|
||||
.Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() });
|
||||
ViewBag.QuestionTypes = questionTypes;
|
||||
|
||||
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
||||
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
||||
|
||||
if (questionnaire == null)
|
||||
|
|
@ -147,21 +151,26 @@ namespace Web.Areas.Admin.Controllers
|
|||
return NotFound(); // Or handle not found case appropriately
|
||||
}
|
||||
|
||||
var viewModel = new QuestionnaireViewModel
|
||||
var viewModel = new EditQuestionnaireViewModel
|
||||
{
|
||||
Id = questionnaire.Id,
|
||||
Title = questionnaire.Title,
|
||||
Description = questionnaire.Description,
|
||||
|
||||
Questions = questionnaire.Questions
|
||||
.Select(q => new Question
|
||||
{
|
||||
Id = q.Id,
|
||||
Text = q.Text,
|
||||
Type = q.Type,
|
||||
|
||||
|
||||
Answers = q.Answers.Select(a => new Answer
|
||||
{
|
||||
Id = a.Id,
|
||||
Text = a.Text
|
||||
Text = a.Text,
|
||||
Question=a.Question
|
||||
|
||||
}).ToList()
|
||||
}).ToList()
|
||||
};
|
||||
|
|
@ -170,12 +179,14 @@ namespace Web.Areas.Admin.Controllers
|
|||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Edit(QuestionnaireViewModel viewModel)
|
||||
public async Task<IActionResult> Edit(EditQuestionnaireViewModel viewModel)
|
||||
{
|
||||
|
||||
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
||||
var questionTypes = Enum.GetValues(typeof(QuestionType))
|
||||
.Cast<QuestionType>()
|
||||
.Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() });
|
||||
ViewBag.QuestionTypes = questionTypes;
|
||||
|
||||
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// Retrieve the existing questionnaire from the database
|
||||
|
|
@ -190,77 +201,149 @@ namespace Web.Areas.Admin.Controllers
|
|||
existingQuestionnaire.Title = viewModel.Title;
|
||||
existingQuestionnaire.Description = viewModel.Description;
|
||||
|
||||
var Question = viewModel.Questions.ToList();
|
||||
|
||||
if(Question.Count()!=0)
|
||||
// Update or add new questions
|
||||
foreach (var questionViewModel in viewModel.Questions)
|
||||
{
|
||||
foreach (var questionViewModel in viewModel.Questions)
|
||||
var existingQuestion = existingQuestionnaire.Questions.SingleOrDefault(q => q.Id == questionViewModel.Id);
|
||||
|
||||
|
||||
if (existingQuestion != null)
|
||||
{
|
||||
existingQuestion.Text = questionViewModel.Text;
|
||||
existingQuestion.Type = questionViewModel.Type;
|
||||
|
||||
var existingQuestion = existingQuestionnaire.Questions.FirstOrDefault(q => q.Id == questionViewModel.Id);
|
||||
bool newAnswersAdded = false; // Flag to track if new answers were added
|
||||
|
||||
if (existingQuestion != null)
|
||||
{
|
||||
existingQuestion.Text = questionViewModel.Text;
|
||||
existingQuestion.Type = questionViewModel.Type;
|
||||
// Check if the question has any answers
|
||||
|
||||
// Update answers
|
||||
// Loop through each answer in the view model
|
||||
foreach (var answerViewModel in questionViewModel.Answers)
|
||||
{
|
||||
// Check if the answer already exists
|
||||
var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id);
|
||||
|
||||
if (existingAnswer != null)
|
||||
if (answerViewModel.Id==0)
|
||||
{
|
||||
existingAnswer.Text = answerViewModel.Text;
|
||||
existingAnswer.QuestionId = answerViewModel.QuestionId;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle adding new answers if necessary
|
||||
|
||||
|
||||
existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
else if(existingAnswer !=null)
|
||||
{
|
||||
|
||||
existingAnswer.Text = answerViewModel.Text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var questionViewModel in viewModel.Questions)
|
||||
{
|
||||
|
||||
var existingQuestion = existingQuestionnaire.Questions.FirstOrDefault(q => q.Id == questionViewModel.Id);
|
||||
//// Add newly answers that exist only in the view model
|
||||
//foreach (var answerViewModel in questionViewModel.Answers.Where(av => existingQuestion.Answers.All(ea => ea.Id != av.Id)))
|
||||
//{
|
||||
// existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
||||
// newAnswersAdded = true; // Set flag to true
|
||||
//}
|
||||
|
||||
if (existingQuestion != null)
|
||||
// If new answers were added, remove any null references
|
||||
if (newAnswersAdded)
|
||||
{
|
||||
existingQuestion.Text = questionViewModel.Text;
|
||||
existingQuestion.Type = questionViewModel.Type;
|
||||
|
||||
// Update answers
|
||||
foreach (var answerViewModel in questionViewModel.Answers)
|
||||
{
|
||||
var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id);
|
||||
|
||||
if (existingAnswer != null)
|
||||
{
|
||||
existingAnswer.Text = answerViewModel.Text;
|
||||
existingAnswer.QuestionId = answerViewModel.QuestionId;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle adding new answers if necessary
|
||||
existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
||||
}
|
||||
}
|
||||
existingQuestion.Answers.RemoveAll(a => a == null);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add new question with its answers
|
||||
var newQuestion = new Question
|
||||
{
|
||||
Text = questionViewModel.Text,
|
||||
Type = questionViewModel.Type,
|
||||
Answers = questionViewModel.Answers?.Select(a => new Answer { Text = a.Text }).ToList() ?? new List<Answer>()
|
||||
};
|
||||
|
||||
existingQuestionnaire.Questions.Add(newQuestion);
|
||||
}
|
||||
|
||||
//if (existingQuestion != null)
|
||||
//{
|
||||
// existingQuestion.Text = questionViewModel.Text;
|
||||
// existingQuestion.Type = questionViewModel.Type;
|
||||
|
||||
// //var answerId = existingQuestion.Answers.Select(x => x.Id).ToList();
|
||||
// // Update or add new answers
|
||||
// //foreach (var answerViewModel in questionViewModel.Answers)
|
||||
// //{
|
||||
// // var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id);
|
||||
|
||||
// // if (existingAnswer != null)
|
||||
// // {
|
||||
// // existingAnswer.Text = answerViewModel.Text;
|
||||
// // }
|
||||
// // else
|
||||
// // {
|
||||
// // // Handle adding new answers if necessary
|
||||
// // existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
||||
// // }
|
||||
// //}
|
||||
// if (questionViewModel.Answers != null)
|
||||
// {
|
||||
// // Update or add new answers
|
||||
// foreach (var answerViewModel in questionViewModel.Answers)
|
||||
// {
|
||||
// var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id);
|
||||
|
||||
// if (existingAnswer != null)
|
||||
// {
|
||||
// // Update existing answer
|
||||
// existingAnswer.Text = answerViewModel.Text;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// foreach (var newanswers in questionViewModel.Answers)
|
||||
// {
|
||||
// existingQuestion.Answers.Add(new Answer { Text = newanswers.Text });
|
||||
// }
|
||||
// // Check if the answer with the same text already exists
|
||||
// //var answerWithSameText = existingQuestion.Answers.FirstOrDefault(a => a.Text == answerViewModel.Text);
|
||||
|
||||
// //if (answerWithSameText == null)
|
||||
// //{
|
||||
// // // Add new answer only if it doesn't exist with the same text
|
||||
|
||||
// //}
|
||||
// //else
|
||||
// //{
|
||||
// // // Optionally handle the case where an answer with the same text already exists
|
||||
// // // You can choose to do nothing, show a message, or take any other action
|
||||
// //}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// // Add new question with its answers
|
||||
// var newQuestion = new Question
|
||||
// {
|
||||
// Text = questionViewModel.Text,
|
||||
// Type = questionViewModel.Type,
|
||||
// Answers = questionViewModel.Answers.Select(a => new Answer { Text = a.Text }).ToList()
|
||||
// };
|
||||
|
||||
// existingQuestionnaire.Questions.Add(newQuestion);
|
||||
//}
|
||||
}
|
||||
|
||||
// Update questions
|
||||
|
||||
|
||||
|
||||
// Remove any questions that are not in the view model
|
||||
var questionIdsInViewModel = viewModel.Questions.Select(q => q.Id);
|
||||
var questionsToRemove = existingQuestionnaire.Questions.Where(q => !questionIdsInViewModel.Contains(q.Id)).ToList();
|
||||
foreach (var questionToRemove in questionsToRemove)
|
||||
{
|
||||
existingQuestionnaire.Questions.Remove(questionToRemove);
|
||||
}
|
||||
|
||||
// Save changes to the database
|
||||
_questionnaire.Update(existingQuestionnaire);
|
||||
|
|
@ -270,7 +353,7 @@ namespace Web.Areas.Admin.Controllers
|
|||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
// If the model state is not valid, return to the edit view with the existing model
|
||||
// If ModelState is not valid, re-display the form with validation errors
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
|
|
@ -318,7 +401,7 @@ namespace Web.Areas.Admin.Controllers
|
|||
_questionnaire.commitAsync();
|
||||
|
||||
return Json(new { success = true, message = "Item deleted successfully" });
|
||||
TempData["Success"] = "Questionnaire deleted successfully";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@
|
|||
</div>
|
||||
|
||||
<button type="button" id="add-question-btn" class="btn btn-md btn-success mb-3"><i class="bi bi-plus-lg"></i> Add New Question </button>
|
||||
|
||||
<hr class="border border-primary border-3 opacity-75">
|
||||
<div class="mt-3">
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
| <a asp-action="Index" class="btn btn-info">Back to list</a>
|
||||
|
|
@ -119,7 +119,7 @@
|
|||
newQuestionHtml += `<div class="container-ms ml-5 mr-5 p-4 px-4 gy-5 ">`;
|
||||
newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
|
||||
newQuestionHtml += `<label class="h3">Create Answers:</label>`;
|
||||
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr class="border border-primary border-3 opacity-75">`;
|
||||
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr <hr class="border border-primary border-1 opacity-35">`;
|
||||
newQuestionHtml += `<label>Answer 1</label>`;
|
||||
newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/>`;
|
||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer mt-2"><i class="bi bi-plus-lg"></i> Add Answer</button>`;
|
||||
|
|
@ -130,7 +130,7 @@
|
|||
|
||||
// Add Remove Question button
|
||||
newQuestionHtml += `<button type="button" class="btn btn-md btn-danger remove-question"><i class="bi bi-trash3-fill"></i> Remove Question </button>`;
|
||||
newQuestionHtml += `<hr <hr class="border border-primary border-3 opacity-75">`
|
||||
newQuestionHtml += `<hr <hr class="border border-primary border-1 opacity-35">`
|
||||
newQuestionHtml += `</div>`;
|
||||
|
||||
$("#questions-container .form-group").append(newQuestionHtml);
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@
|
|||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p id="deleteMessage">Are you sure you want to delete this item?</p>
|
||||
<p id="deleteMessage">Are you sure you want to delete this <span class="badge badge-danger">@Model.Title</span>questionnaire</p>
|
||||
<p class="text-danger">If you delete, you can't recover it.</p>
|
||||
<input type="text" class="form-control" id="deleteConfirmation" placeholder="Type the questionnaire name to confirm">
|
||||
</div>
|
||||
|
|
@ -68,27 +68,7 @@
|
|||
</div>
|
||||
|
||||
|
||||
@* <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="deleteModalLabel">Delete Confirmation</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Are you sure you want to delete this item?</p>
|
||||
<p class="text-danger">if you delete, you can't recover back</p>
|
||||
<input type="text" class="form-control" id="deleteConfirmation" placeholder="Type the questionnaire name to confirm">
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-danger" id="deleteButton" disabled>Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div> *@
|
||||
|
||||
|
||||
<!-- Delete Confirmation Modal -->
|
||||
|
||||
|
|
@ -108,60 +88,10 @@
|
|||
CKEDITOR.replace("Description");
|
||||
</script>
|
||||
@{
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
}
|
||||
<script>
|
||||
// $(document).ready(function () {
|
||||
// var itemId = @Model.Id; // Assuming you can get the item ID from the model
|
||||
|
||||
// // Enable delete button when the input matches the item name
|
||||
// $('#deleteConfirmation').on('input', function () {
|
||||
// var itemName = '@Model.Title'; // Item name from the model
|
||||
// var inputText = $(this).val().trim().toLowerCase();
|
||||
// var isMatch = inputText === itemName.toLowerCase();
|
||||
// $('#deleteButton').prop('disabled', !isMatch);
|
||||
// });
|
||||
|
||||
// // Clear input and disable button when modal is hidden
|
||||
// $('#deleteModal').on('hidden.bs.modal', function () {
|
||||
// $('#deleteConfirmation').val('');
|
||||
// $('#deleteButton').prop('disabled', true);
|
||||
// });
|
||||
|
||||
// // Delete button click event
|
||||
// $('#deleteButton').on('click', function () {
|
||||
// // Make an AJAX request to delete the item
|
||||
// $.ajax({
|
||||
// url: '/admin/Questionnaire/Delete/' + itemId,
|
||||
// type: 'POST', // or 'DELETE' if you have a dedicated delete action
|
||||
// success: function (result) {
|
||||
// // Hide the confirmation details
|
||||
// $('#deleteConfirmation').hide();
|
||||
// $('#deleteButton').hide();
|
||||
// // Show the success message
|
||||
// $('#deleteMessage').text('the questionnaire deleted successfully.').show();
|
||||
// // Show the modal
|
||||
// $('#deleteModal').modal('show');
|
||||
// // Automatically close the modal after 4 seconds
|
||||
// setTimeout(function () {
|
||||
// $('#deleteModal').modal('hide');
|
||||
// // Redirect to the index action method after closing the modal
|
||||
// window.location.href = '/admin/Questionnaire/Index';
|
||||
// }, 3000);
|
||||
// },
|
||||
// error: function (error) {
|
||||
// // Handle error
|
||||
// $('#deleteMessage').text('Failed to delete item.').show();
|
||||
// // Show the modal
|
||||
// $('#deleteModal').modal('show');
|
||||
// // Automatically close the modal after 4 seconds
|
||||
// setTimeout(function () {
|
||||
// $('#deleteModal').modal('hide');
|
||||
// }, 4000);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
$(document).ready(function () {
|
||||
var itemId = @Model.Id; // Assuming you can get the item ID from the model
|
||||
|
||||
|
|
@ -170,6 +100,7 @@
|
|||
var itemName = '@Model.Title'; // Item name from the model
|
||||
var inputText = $(this).val().trim().toLowerCase();
|
||||
var isMatch = inputText === itemName.toLowerCase();
|
||||
// var space = $(this).val.space();
|
||||
$('#deleteButton').prop('disabled', !isMatch);
|
||||
});
|
||||
|
||||
|
|
@ -198,7 +129,7 @@
|
|||
$('#deleteModal').modal('hide');
|
||||
// Redirect to the index action method after closing the modal
|
||||
window.location.href = '/admin/Questionnaire/Index';
|
||||
}, 3000);
|
||||
}, 1000);
|
||||
},
|
||||
error: function (error) {
|
||||
// Handle error
|
||||
|
|
@ -208,7 +139,7 @@
|
|||
// Automatically close the modal after 4 seconds
|
||||
setTimeout(function () {
|
||||
$('#deleteModal').modal('hide');
|
||||
}, 3000);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -217,54 +148,6 @@
|
|||
|
||||
</script>
|
||||
|
||||
@* <script>
|
||||
$(document).ready(function () {
|
||||
var itemId = @Model.Id; // Assuming you can get the item ID from the model
|
||||
|
||||
// Enable delete button when the input matches the item name
|
||||
$('#deleteConfirmation').on('input', function () {
|
||||
var itemName = '@Model.Title'; // Item name from the model
|
||||
var inputText = $(this).val().trim().toLowerCase();
|
||||
var isMatch = inputText === itemName.toLowerCase();
|
||||
$('#deleteButton').prop('disabled', !isMatch);
|
||||
});
|
||||
|
||||
// Clear input and disable button when modal is hidden
|
||||
$('#deleteModal').on('hidden.bs.modal', function () {
|
||||
$('#deleteConfirmation').val('');
|
||||
$('#deleteButton').prop('disabled', true);
|
||||
});
|
||||
|
||||
// Delete button click event
|
||||
$('#deleteButton').on('click', function () {
|
||||
// Make an AJAX request to delete the item
|
||||
$.ajax({
|
||||
url: '/admin/Questionnaire/Delete/' + itemId,
|
||||
type: 'POST', // or 'DELETE' if you have a dedicated delete action
|
||||
success: function (result) {
|
||||
// Handle success
|
||||
$('#deleteMessage').text('Item deleted successfully.');
|
||||
$('#deleteModal').modal('show');
|
||||
setTimeout(function () {
|
||||
$('#deleteModal').modal('hide');
|
||||
// Redirect to the index action method after closing the modal
|
||||
window.location.href = '/admin/Questionnaire/Index';
|
||||
}, 3000);
|
||||
},
|
||||
error: function (error) {
|
||||
// Handle error
|
||||
$('#deleteMessage').text('Failed to delete item.');
|
||||
$('#deleteModal').modal('show');
|
||||
setTimeout(function () {
|
||||
$('#deleteModal').modal('hide');
|
||||
}, 3000);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
</script> *@
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
@model QuestionnaireViewModel
|
||||
@model EditQuestionnaireViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Edit";
|
||||
|
|
@ -7,288 +7,126 @@
|
|||
<div class="container mt-4">
|
||||
<div class="card justify-content-center p-4 shadow rounded">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Create Survey</h5>
|
||||
<h5 class="card-title">Edit Survey</h5>
|
||||
|
||||
<div class="row ">
|
||||
<!-- 12 columns for textboxes -->
|
||||
<div class="row">
|
||||
<form asp-action="Edit">
|
||||
<input type="hidden" asp-for="Id" />
|
||||
<div class="form-group">
|
||||
<label asp-for="Title" class="control-label"></label>
|
||||
<input asp-for="Title" class="form-control" />
|
||||
<span asp-validation-for="Title" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Description" class="control-label"></label>
|
||||
<textarea asp-for="Description" class="form-control"></textarea>
|
||||
<span asp-validation-for="Description" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<form asp-action="Edit" asp-controller="Questionnaire">
|
||||
<div asp-validation-summary="All" class="text-danger"></div>
|
||||
|
||||
<div class="mb-3 col-12">
|
||||
<label asp-for="Title" class="control-label"></label>
|
||||
<input asp-for="Title" class="form-control" />
|
||||
<span asp-validation-for="Title" class="text-danger"></span>
|
||||
<div id="questionsContainer">
|
||||
@for (int i = 0; i < Model.Questions.Count; i++)
|
||||
{
|
||||
<div class="question">
|
||||
<input type="hidden" asp-for="Questions[i].Id" />
|
||||
<div class="form-group">
|
||||
<label asp-for="Questions[i].Text" class="control-label"></label>
|
||||
<input asp-for="Questions[i].Text" class="form-control" />
|
||||
<span asp-validation-for="Questions[i].Text" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Questions[i].Type" class="control-label"></label>
|
||||
<select asp-for="Questions[i].Type" asp-items="@ViewBag.QuestionTypes" class="form-control"></select>
|
||||
<span asp-validation-for="Questions[i].Type" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="answers">
|
||||
@for (int j = 0; j < Model.Questions[i].Answers.Count; j++)
|
||||
{
|
||||
<div class="answer">
|
||||
<input type="hidden" asp-for="Questions[i].Answers[j].Id" />
|
||||
<div class="form-group">
|
||||
<label asp-for="Questions[i].Answers[j].Text" class="control-label"></label>
|
||||
<input asp-for="Questions[i].Answers[j].Text" class="form-control" />
|
||||
<span asp-validation-for="Questions[i].Answers[j].Text" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="mb-3 col-12">
|
||||
<label asp-for="Description" class="control-label"></label>
|
||||
<textarea asp-for="Description" class="form-control"></textarea>
|
||||
<span asp-validation-for="Description" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="container p-5 px-4 gy-5 shadow bg-body-tertiary rounded">
|
||||
<button type="button" class="btn btn-danger removeAnswer">Remove Answer</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary addAnswer">Add Answer</button>
|
||||
<button type="button" class="btn btn-danger removeQuestion">Remove Question</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div id="questions-container">
|
||||
</div>
|
||||
|
||||
<h3>Create Questions</h3>
|
||||
<div class="form-group px-4 gy-5 ">
|
||||
@for (int i = 0; i < Model.Questions?.Count; i++)
|
||||
{
|
||||
<div class="question-group" data-question-index="@i">
|
||||
<label>Question @(i + 1)</label>
|
||||
<textarea name="Questions[@i].Text" class="form-control">@Model.Questions[i].Text</textarea>
|
||||
<select name="Questions[@i].Type" asp-items="ViewBag.QuestionTypes" class="form-control question-type">
|
||||
<button type="button" class="btn btn-primary" id="addQuestion">Add Question</button>
|
||||
<button type="submit" class="btn btn-primary">Update</button>
|
||||
</form>
|
||||
|
||||
<!-- Include options for question types... -->
|
||||
<div class="container-sm"></div>
|
||||
</select>
|
||||
|
||||
<div class="answers-container">
|
||||
<label>Answers:</label>
|
||||
@for (int j = 0; j < Model.Questions?[i].Answers?.Count; j++)
|
||||
{
|
||||
<div class="answer-group">
|
||||
<input type="text" name="Questions[@i].Answers[@j].Text" class="form-control" value="@Model.Questions?[i]?.Answers?[j]?.Text" />
|
||||
<button type="button" class="btn btn-sm btn-danger remove-answer">Remove Answer</button>
|
||||
</div>
|
||||
}
|
||||
<button type="button" class="btn btn-sm btn-success add-answer">Add Answer</button>
|
||||
<!-- Add a hidden field to address the required field validation -->
|
||||
<input type="hidden" name="Questions[@i].Answers" />
|
||||
</div>
|
||||
<button type="button" class="btn btn-sm btn-danger remove-question">Remove Question</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="button" id="add-question-btn" class="btn btn-md btn-primary mb-3">Add New Question</button>
|
||||
|
||||
<div class="mt-3">
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
| <a asp-action="Index" class="btn btn-info">Back to list</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr />
|
||||
<hr />
|
||||
|
||||
|
||||
|
||||
|
||||
@section Scripts {
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
|
||||
<script>
|
||||
CKEDITOR.replace("Description");
|
||||
</script>
|
||||
$(document).ready(function () {
|
||||
// Function to add a new answer
|
||||
$(document).on('click', '.addAnswer', function () {
|
||||
var questionContainer = $(this).closest('.question');
|
||||
var newQuestionIndex = questionContainer.index();
|
||||
|
||||
@{
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
}
|
||||
// Correctly find the length of answers for the specific question
|
||||
var newAnswerIndex = questionContainer.find('.answers .form-group').length;
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
|
||||
var answerHtml = `
|
||||
<div class="form-group">
|
||||
<label class="control-label">Answer Text</label>
|
||||
<input type="text" name="Questions[${newQuestionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" />
|
||||
</div>`;
|
||||
|
||||
@* <script>
|
||||
$(document).ready(function () {
|
||||
var questionIndex = @Model.Questions?.Count;
|
||||
|
||||
$("#add-question-btn").click(function () {
|
||||
var newQuestionHtml = `
|
||||
<div class="question-group" data-question-index="${questionIndex}">
|
||||
<label>Question ${questionIndex + 1}</label>
|
||||
<textarea name="Questions[${questionIndex}].Text" class="form-control"></textarea>
|
||||
<br>
|
||||
<label class=h5>Select question type</label>
|
||||
<select name="Questions[${questionIndex}].Type" class="form-control question-type">`;
|
||||
|
||||
var questionTypes = @Html.Raw(Json.Serialize(Enum.GetNames(typeof(QuestionType))));
|
||||
|
||||
for (var i = 0; i < questionTypes.length; i++) {
|
||||
newQuestionHtml += `<option value="${questionTypes[i]}">${questionTypes[i]}</option>`;
|
||||
}
|
||||
|
||||
newQuestionHtml += `</select>`;
|
||||
|
||||
// Add answers input fields
|
||||
|
||||
newQuestionHtml += `<div class="container-ms ml-5 mr-5 p-4 px-4 gy-5 ">`;
|
||||
newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
|
||||
newQuestionHtml += `<label class="h3">Create Answers:</label>`;
|
||||
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr class="border border-primary border-3 opacity-75">`;
|
||||
newQuestionHtml += `<label>Answer 1</label>`;
|
||||
newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/>`;
|
||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer mt-2">Add Answer</button>`;
|
||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-danger remove-answer mt-2" style="display:none">Remove Answer</button><br><hr>`;
|
||||
newQuestionHtml += `</div> `;
|
||||
newQuestionHtml += `</div>`;
|
||||
newQuestionHtml += `</div> `;
|
||||
|
||||
// Add Remove Question button
|
||||
newQuestionHtml += `<button type="button" class="btn btn-md btn-danger remove-question">Remove Question</button>`;
|
||||
newQuestionHtml += `<hr <hr class="border border-primary border-3 opacity-75">`
|
||||
newQuestionHtml += `</div>`;
|
||||
|
||||
$("#questions-container .form-group").append(newQuestionHtml);
|
||||
questionIndex++;
|
||||
});
|
||||
|
||||
// Add answer dynamically without "Add Answer" button
|
||||
$("#questions-container").on("click", ".add-answer", function () {
|
||||
var questionIndex = $(this).closest('.answers-container').data('question-index');
|
||||
var answerIndex = $(this).closest('.answers-container').find('.answer-group').length;
|
||||
var answerGroup = $(this).closest('.answers-container').find('.answer-group:first').clone();
|
||||
answerGroup.find('input').val(''); // Clear input values
|
||||
|
||||
answerGroup.find('.add-answer').hide(); // Hide the "Add Answer" button
|
||||
answerGroup.find('.remove-answer').show(); // Show the "Remove Answer" button
|
||||
|
||||
answerGroup.data('answer-index', answerIndex);
|
||||
answerGroup.find('label').text(`Answer ${answerIndex + 1}`);
|
||||
answerGroup.find('input').attr('name', `Questions[${questionIndex}].Answers[${answerIndex}].Text`);
|
||||
$(this).closest('.answers-container').append(answerGroup);
|
||||
});
|
||||
|
||||
// Remove answer dynamically
|
||||
$("#questions-container").on("click", ".remove-answer", function () {
|
||||
$(this).closest('.answer-group').remove();
|
||||
});
|
||||
|
||||
// Remove question dynamically
|
||||
$("#questions-container").on("click", ".remove-question", function () {
|
||||
$(this).closest('.question-group').remove();
|
||||
});
|
||||
});
|
||||
</script> *@
|
||||
@* <script>
|
||||
$(document).ready(function () {
|
||||
var questionIndex = @Model.Questions?.Count;
|
||||
|
||||
$("#add-question-btn").click(function () {
|
||||
var newQuestionHtml = `
|
||||
<div class="question-group" data-question-index="${questionIndex}">
|
||||
<label>Question ${questionIndex + 1}</label>
|
||||
<textarea name="Questions[${questionIndex}].Text" class="form-control"></textarea>
|
||||
<label class=h5>Select question type</label>
|
||||
<select name="Questions[${questionIndex}].Type" class="form-control question-type">`;
|
||||
|
||||
var questionTypes = @Html.Raw(Json.Serialize(Enum.GetNames(typeof(QuestionType))));
|
||||
|
||||
for (var i = 0; i < questionTypes.length; i++) {
|
||||
newQuestionHtml += `<option value="${questionTypes[i]}">${questionTypes[i]}</option>`;
|
||||
}
|
||||
|
||||
newQuestionHtml += `</select>`;
|
||||
|
||||
// Add answers input fields
|
||||
newQuestionHtml += `<div class="container-ms ml-5 mr-5 p-4 px-4 gy-5 ">`;
|
||||
newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
|
||||
newQuestionHtml += `<label class="h3">Create Answers:</label>`;
|
||||
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr class="border border-primary border-3 opacity-75">`;
|
||||
newQuestionHtml += `<label>Answer 1</label>`;
|
||||
newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/>`;
|
||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer mt-2">Add Answer</button>`;
|
||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-danger remove-answer mt-2" style="display:none">Remove Answer</button><br><hr>`;
|
||||
newQuestionHtml += `</div> `;
|
||||
newQuestionHtml += `</div>`;
|
||||
newQuestionHtml += `</div> `;
|
||||
|
||||
// Add Remove Question button
|
||||
newQuestionHtml += `<button type="button" class="btn btn-md btn-danger remove-question">Remove Question</button>`;
|
||||
newQuestionHtml += `<hr class="border border-primary border-3 opacity-75">`
|
||||
newQuestionHtml += `</div>`;
|
||||
|
||||
$("#questions-container .form-group").append(newQuestionHtml);
|
||||
questionIndex++;
|
||||
});
|
||||
|
||||
// Add answer dynamically without "Add Answer" button
|
||||
$("#questions-container").on("click", ".add-answer", function () {
|
||||
var questionIndex = $(this).closest('.answers-container').data('question-index');
|
||||
var answerIndex = $(this).closest('.answers-container').find('.answer-group').length;
|
||||
var answerGroup = $(this).closest('.answers-container').find('.answer-group:first').clone();
|
||||
answerGroup.find('input').val(''); // Clear input values
|
||||
|
||||
answerGroup.find('.add-answer').hide(); // Hide the "Add Answer" button
|
||||
answerGroup.find('.remove-answer').show(); // Show the "Remove Answer" button
|
||||
|
||||
answerGroup.data('answer-index', answerIndex);
|
||||
answerGroup.find('label').text(`Answer ${answerIndex + 1}`);
|
||||
answerGroup.find('input').attr('name', `Questions[${questionIndex}].Answers[${answerIndex}].Text`);
|
||||
$(this).closest('.answers-container').append(answerGroup);
|
||||
});
|
||||
|
||||
// Remove answer dynamically
|
||||
$("#questions-container").on("click", ".remove-answer", function () {
|
||||
$(this).closest('.answer-group').remove();
|
||||
});
|
||||
|
||||
// Remove question dynamically
|
||||
$("#questions-container").on("click", ".remove-question", function () {
|
||||
$(this).closest('.question-group').remove();
|
||||
});
|
||||
// Append the new answer to the specific question
|
||||
questionContainer.find('.answers').append(answerHtml);
|
||||
});
|
||||
|
||||
// Function to remove an answer
|
||||
$(document).on('click', '.removeAnswer', function () {
|
||||
$(this).closest('.form-group').remove();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
@* this script is working for adding mutiple answers for a question *@
|
||||
|
||||
|
||||
</script> *@
|
||||
|
||||
|
||||
<script>
|
||||
@* <script>
|
||||
$(document).ready(function () {
|
||||
// Handle the "Add Question" button
|
||||
$("#add-question-btn").click(function () {
|
||||
var questionIndex = $("#questions-container .question-group").length;
|
||||
// Function to add a new answer
|
||||
$(document).on('click', '.addAnswer', function () {
|
||||
var questionContainer = $(this).closest('.question');
|
||||
var newQuestionIndex = questionContainer.index();
|
||||
var newAnswerIndex = questionContainer.find('.answers .form-group').length; // Correct way to find the length
|
||||
|
||||
var newQuestionHtml = `
|
||||
<div class="question-group">
|
||||
<label>Question ${questionIndex + 1}</label>
|
||||
<textarea name="Questions[${questionIndex}].Text" class="form-control"></textarea>
|
||||
<br>
|
||||
<select name="Questions[${questionIndex}].Type" class="form-control question-type">
|
||||
<option value="">Select Question Type</option>
|
||||
<!-- Add options based on your question type enum -->
|
||||
</select>
|
||||
|
||||
<div class="answers-container">
|
||||
<label>Answers:</label>
|
||||
<div class="answer-group">
|
||||
<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="Answer 1" />
|
||||
<button type="button" class="btn btn-sm btn-success add-answer">Add Answer</button>
|
||||
<button type="button" class="btn btn-sm btn-danger remove-answer" style="display:none">Remove Answer</button>
|
||||
</div>
|
||||
<input type="hidden" name="Questions[${questionIndex}].Answers" />
|
||||
</div>
|
||||
var answerHtml = `
|
||||
<div class="form-group">
|
||||
<label class="control-label">Answer Text</label>
|
||||
<input type="text" name="Questions[${newQuestionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" />
|
||||
</div>`;
|
||||
|
||||
$("#questions-container").append(newQuestionHtml);
|
||||
$(this).siblings('.answers').append(answerHtml);
|
||||
});
|
||||
|
||||
// Handle the "Add Answer" button
|
||||
$("#questions-container").on("click", ".add-answer", function () {
|
||||
var answerGroup = $(this).closest('.answers-container').find('.answer-group:first').clone();
|
||||
answerGroup.find('input').val(''); // Clear input values
|
||||
answerGroup.find('.add-answer').hide(); // Hide the "Add Answer" button
|
||||
answerGroup.find('.remove-answer').show(); // Show the "Remove Answer" button
|
||||
$(this).closest('.answers-container').append(answerGroup);
|
||||
});
|
||||
|
||||
// Handle the "Remove Answer" button
|
||||
$("#questions-container").on("click", ".remove-answer", function () {
|
||||
$(this).closest('.answer-group').remove();
|
||||
});
|
||||
|
||||
// Handle the "Remove Question" button
|
||||
$("#questions-container").on("click", ".remove-question", function () {
|
||||
$(this).closest('.question-group').remove();
|
||||
// Function to remove an answer
|
||||
$(document).on('click', '.removeAnswer', function () {
|
||||
$(this).closest('.form-group').remove();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
*@
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,17 +12,18 @@
|
|||
<div class="card-body">
|
||||
<h4 class="card-title">Questionnaire list</h4>
|
||||
<p>
|
||||
<a asp-action="Create" class="btn btn-primary">Create New</a>
|
||||
<a asp-action="Create" class="btn btn-primary"><span><i class="bi bi-plus-square-fill"></i></span> Create New</a>
|
||||
</p>
|
||||
<table class="table table-responsive d-block">
|
||||
<table class="table table-responsive d-block table-light table-hover">
|
||||
<thead class="w-auto">
|
||||
<tr>
|
||||
|
||||
<th scope="col">Id</th>
|
||||
<th scope="col">Title</th>
|
||||
<th scope="col">Description</th>
|
||||
<th scope="col">Number of questions</th>
|
||||
<th scope="col">Total Question</th>
|
||||
<th scope="col"> <span class="badge badge-primary">Questions</span> | <span class="badge badge-info">Type</span> | <span class="badge badge-success">Answers </span></th>
|
||||
|
||||
<th scope="col" class="d-flex justify-content-end">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
|
@ -33,22 +34,29 @@
|
|||
|
||||
<td>@item.Id</td>
|
||||
<td> @item.Title</td>
|
||||
<td>@item.Description</td>
|
||||
<td>@Html.Raw(item.Description)</td>
|
||||
|
||||
<td>
|
||||
<span class="badge p-1 m-1 bg-primary shadow-sm"> Total Questions:@item.Questions?.Count()</span>
|
||||
@* <button type="button" class="btn btn-primary btn-sm">
|
||||
|
||||
</button> *@
|
||||
<span class="badge shadow rounded text-bg-primary p-2">
|
||||
Questions <span class="badge text-bg-secondary shadow rounded p-1">@item.Questions?.Count()</span>
|
||||
</span>
|
||||
|
||||
</td>
|
||||
<td class="h5">
|
||||
|
||||
@foreach (var question in item.Questions)
|
||||
@foreach (var question in item.Questions.Take(1))
|
||||
{
|
||||
|
||||
<span class="badge p-1 m-1 bg-primary shadow-sm"> Question:@question.Text</span>
|
||||
<span class="badge p-1 m-1 bg-primary shadow-sm"> Question:@question.Text</span>
|
||||
<span class="badge p-1 m-1 bg-info shadow-sm">Type: @question.Type</span>
|
||||
foreach (var answer in question.Answers)
|
||||
{
|
||||
<span class="badge p-1 m-1 bg-success shadow-sm"> Asnwer:@answer.Text</span>
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
23
Web/ViewModel/QuestionnaireVM/EditQuestionnaireViewModel.cs
Normal file
23
Web/ViewModel/QuestionnaireVM/EditQuestionnaireViewModel.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using Model;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Web.ViewModel.QuestionnaireVM
|
||||
{
|
||||
public class EditQuestionnaireViewModel
|
||||
{
|
||||
public EditQuestionnaireViewModel()
|
||||
{
|
||||
Questions = new List<Question>();
|
||||
}
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
|
||||
[Display(Name ="Questionnaire title")]
|
||||
public string? Title { get; set; }
|
||||
[Required]
|
||||
public string? Description { get; set; }
|
||||
|
||||
public List<Question>? Questions { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,9 @@
|
|||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="Admin" asp-controller="Admin" asp-action="Index">Admin</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue