Questionnaire details configured
This commit is contained in:
parent
9938e696f6
commit
69810186eb
9 changed files with 315 additions and 142 deletions
|
|
@ -69,6 +69,7 @@ namespace Data
|
||||||
.HasKey(a => a.Id);
|
.HasKey(a => a.Id);
|
||||||
|
|
||||||
modelBuilder.Entity<QuestionTypeEntities>()
|
modelBuilder.Entity<QuestionTypeEntities>()
|
||||||
|
|
||||||
.HasKey(t => t.Id);
|
.HasKey(t => t.Id);
|
||||||
|
|
||||||
base.OnModelCreating(modelBuilder);
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ namespace Model
|
||||||
public class Answer
|
public class Answer
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
public string? Text { get; set; }
|
public string? Text { get; set; }
|
||||||
public int QuestionId { get; set; } // Foreign key for Question
|
public int QuestionId { get; set; } // Foreign key for Question
|
||||||
[ForeignKey("QuestionId")]
|
[ForeignKey("QuestionId")]
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ namespace Services.Implemnetation
|
||||||
|
|
||||||
public void Update(Questionnaire questionnaire)
|
public void Update(Questionnaire questionnaire)
|
||||||
{
|
{
|
||||||
|
|
||||||
_context.Questionnaires.Update(questionnaire);
|
_context.Questionnaires.Update(questionnaire);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,6 @@ namespace Web.Areas.Admin.Controllers
|
||||||
Questions = new List<Question>(),
|
Questions = new List<Question>(),
|
||||||
Answers = new List<Answer>()
|
Answers = new List<Answer>()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -157,19 +155,25 @@ namespace Web.Areas.Admin.Controllers
|
||||||
Title = questionnaire.Title,
|
Title = questionnaire.Title,
|
||||||
Description = questionnaire.Description,
|
Description = questionnaire.Description,
|
||||||
|
|
||||||
|
|
||||||
Questions = questionnaire.Questions
|
Questions = questionnaire.Questions
|
||||||
.Select(q => new Question
|
.Select(q => new Question
|
||||||
{
|
{
|
||||||
Id = q.Id,
|
Id = q.Id,
|
||||||
Text = q.Text,
|
Text = q.Text,
|
||||||
Type = q.Type,
|
Type = q.Type,
|
||||||
|
QuestionnaireId=q.QuestionnaireId,
|
||||||
|
|
||||||
|
|
||||||
Answers = q.Answers.Select(a => new Answer
|
Answers = q.Answers.Select(a => new Answer
|
||||||
{
|
{
|
||||||
Id = a.Id,
|
Id = a.Id,
|
||||||
Text = a.Text,
|
Text = a.Text,
|
||||||
Question=a.Question
|
Question=a.Question,
|
||||||
|
QuestionId=a.QuestionId
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}).ToList()
|
}).ToList()
|
||||||
}).ToList()
|
}).ToList()
|
||||||
|
|
@ -204,7 +208,7 @@ namespace Web.Areas.Admin.Controllers
|
||||||
// Update or add new questions
|
// 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);
|
var existingQuestion = existingQuestionnaire.Questions.FirstOrDefault(q => q.Id == questionViewModel.Id);
|
||||||
|
|
||||||
|
|
||||||
if (existingQuestion != null)
|
if (existingQuestion != null)
|
||||||
|
|
@ -212,11 +216,8 @@ namespace Web.Areas.Admin.Controllers
|
||||||
existingQuestion.Text = questionViewModel.Text;
|
existingQuestion.Text = questionViewModel.Text;
|
||||||
existingQuestion.Type = questionViewModel.Type;
|
existingQuestion.Type = questionViewModel.Type;
|
||||||
|
|
||||||
bool newAnswersAdded = false; // Flag to track if new answers were added
|
|
||||||
|
|
||||||
// Check if the question has any answers
|
|
||||||
|
|
||||||
// Loop through each answer in the view model
|
|
||||||
foreach (var answerViewModel in questionViewModel.Answers)
|
foreach (var answerViewModel in questionViewModel.Answers)
|
||||||
{
|
{
|
||||||
// Check if the answer already exists
|
// Check if the answer already exists
|
||||||
|
|
@ -225,37 +226,33 @@ namespace Web.Areas.Admin.Controllers
|
||||||
if (answerViewModel.Id==0)
|
if (answerViewModel.Id==0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text });
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else if (string.IsNullOrEmpty(answerViewModel.Text))
|
||||||
|
{
|
||||||
|
|
||||||
|
existingQuestion.Answers.Remove(existingAnswer);
|
||||||
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
else if(existingAnswer !=null)
|
else if(existingAnswer !=null)
|
||||||
{
|
{
|
||||||
|
|
||||||
existingAnswer.Text = answerViewModel.Text;
|
existingAnswer.Text = answerViewModel.Text;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//// 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 new answers were added, remove any null references
|
|
||||||
if (newAnswersAdded)
|
|
||||||
{
|
|
||||||
existingQuestion.Answers.RemoveAll(a => a == null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Add new question with its answers
|
|
||||||
var newQuestion = new Question
|
var newQuestion = new Question
|
||||||
{
|
{
|
||||||
Text = questionViewModel.Text,
|
Text = questionViewModel.Text,
|
||||||
|
|
@ -338,18 +335,17 @@ namespace Web.Areas.Admin.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any questions that are not in the view model
|
// Remove any questions that are not in the view model
|
||||||
var questionIdsInViewModel = viewModel.Questions.Select(q => q.Id);
|
//var questionIdsInViewModel = viewModel.Questions.Select(q => q.Id);
|
||||||
var questionsToRemove = existingQuestionnaire.Questions.Where(q => !questionIdsInViewModel.Contains(q.Id)).ToList();
|
//var questionsToRemove = existingQuestionnaire.Questions.Where(q => !questionIdsInViewModel.Contains(q.Id)).ToList();
|
||||||
foreach (var questionToRemove in questionsToRemove)
|
//foreach (var questionToRemove in questionsToRemove)
|
||||||
{
|
//{
|
||||||
existingQuestionnaire.Questions.Remove(questionToRemove);
|
// existingQuestionnaire.Questions.Remove(questionToRemove);
|
||||||
}
|
//}
|
||||||
|
|
||||||
// Save changes to the database
|
// Save changes to the database
|
||||||
_questionnaire.Update(existingQuestionnaire);
|
_questionnaire.Update(existingQuestionnaire);
|
||||||
TempData["Success"] = "Questionnaire updated successfully";
|
|
||||||
await _questionnaire.commitAsync();
|
await _questionnaire.commitAsync();
|
||||||
|
TempData["Success"] = "Questionnaire updated successfully";
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -404,5 +400,40 @@ namespace Web.Areas.Admin.Controllers
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Details(int id)
|
||||||
|
{
|
||||||
|
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
||||||
|
|
||||||
|
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
||||||
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
||||||
|
|
||||||
|
if (questionnaire == null)
|
||||||
|
{
|
||||||
|
return NotFound(); // Or handle not found case appropriately
|
||||||
|
}
|
||||||
|
|
||||||
|
var viewModel = new QuestionnaireViewModel
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}).ToList()
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
|
||||||
|
return View(viewModel);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,12 +26,15 @@
|
||||||
<textarea asp-for="Description" class="form-control"></textarea>
|
<textarea asp-for="Description" class="form-control"></textarea>
|
||||||
<span asp-validation-for="Description" class="text-danger"></span>
|
<span asp-validation-for="Description" class="text-danger"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="container p-5 px-4 gy-5 shadow bg-body-tertiary rounded">
|
<div class="container p-5 shadow bg-body-tertiary rounded">
|
||||||
|
|
||||||
|
<div id="questions-container" class="mx-md-3 mx-lg-3 px-md-3 px-lg-3 mx-sm-0 px-sm-0">
|
||||||
|
|
||||||
|
<h3 class="text-primary font-weight-bold">Create Questions</h3>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
|
||||||
<div id="questions-container">
|
|
||||||
|
|
||||||
<h3>Create Questions</h3>
|
|
||||||
<div class="form-group px-4 gy-5 ">
|
|
||||||
@for (int i = 0; i < Model.Questions?.Count; i++)
|
@for (int i = 0; i < Model.Questions?.Count; i++)
|
||||||
{
|
{
|
||||||
<div class="question-group" data-question-index="@i">
|
<div class="question-group" data-question-index="@i">
|
||||||
|
|
@ -49,28 +52,32 @@
|
||||||
{
|
{
|
||||||
<div class="answer-group">
|
<div class="answer-group">
|
||||||
<input type="text" name="Questions[@i].Answers[@j].Text" class="form-control" value="@Model.Questions?[i]?.Answers?[j]?.Text" />
|
<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 <i class="bi bi-trash3-fill"></i></button>
|
<button type="button" class="btn btn-sm btn-danger remove-answer"> </button>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<button type="button" class="btn btn-sm btn-success add-answer"><i class="bi bi-plus-lg"></i> Add Answer</button>
|
<button type="button" class="btn btn-sm btn-success add-answer"><i class="bi bi-plus-lg"></i> Add Answer</button>
|
||||||
<!-- Add a hidden field to address the required field validation -->
|
<!-- Add a hidden field to address the required field validation -->
|
||||||
<input type="hidden" name="Questions[@i].Answers" />
|
<input type="hidden" name="Questions[@i].Answers" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="button" class="btn btn-sm btn-danger remove-question">Remove Question <i class="bi bi-trash3-fill"></i></button>
|
<button type="button" class="btn btn-sm btn-danger remove-question">Remove Question <i class="bi bi-trash3-fill"></i></button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</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>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-3 card-footer">
|
||||||
|
<button type="button" id="add-question-btn" class="btn btn-md btn-success"><i class="bi bi-plus-lg"></i>Add New Question</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
|
| <a asp-action="Index" class="btn btn-info">Back to list</a>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -116,43 +123,55 @@
|
||||||
|
|
||||||
// Add answers input fields
|
// Add answers input fields
|
||||||
|
|
||||||
newQuestionHtml += `<div class="container-ms ml-5 mr-5 p-4 px-4 gy-5 ">`;
|
newQuestionHtml += `<div class="container-ms mx-5 py-2 px-5 ">`;
|
||||||
newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
|
newQuestionHtml += `<div class="answers-container" data-question-index="${questionIndex}"><br>`;
|
||||||
newQuestionHtml += `<label class="h3">Create Answers:</label>`;
|
newQuestionHtml += `<label class="h3">Create Answers:</label>`;
|
||||||
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hr <hr class="border border-primary border-1 opacity-35">`;
|
newQuestionHtml += `<div class="answer-group" data-answer-index="0"><hrclass="border border-primary border-1 opacity-35">`;
|
||||||
newQuestionHtml += `<label>Answer 1</label>`;
|
newQuestionHtml += `<label>Answer 1</label>`;
|
||||||
newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/>`;
|
newQuestionHtml += `<input type="text" name="Questions[${questionIndex}].Answers[0].Text" class="form-control" placeholder="new answer"/><br>`;
|
||||||
|
|
||||||
newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer mt-2"><i class="bi bi-plus-lg"></i> Add Answer</button>`;
|
newQuestionHtml += `<button type="button" class="btn btn-sm btn-success add-answer mt-2"><i class="bi bi-plus-lg"></i> Add Answer</button>`;
|
||||||
newQuestionHtml += `<button type="button" class="btn btn-md btn-danger remove-answer mt-2" style="display:none"> <i class="bi bi-trash3-fill"></i></button><br><hr>`;
|
newQuestionHtml += `<hr class="border m-2">`
|
||||||
newQuestionHtml += `</div> `;
|
newQuestionHtml += `</div> `;
|
||||||
newQuestionHtml += `</div>`;
|
newQuestionHtml += `</div>`;
|
||||||
newQuestionHtml += `</div> `;
|
newQuestionHtml += `</div> `;
|
||||||
|
|
||||||
// Add Remove Question button
|
// 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 += `<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-1 opacity-35">`
|
newQuestionHtml += `<hr class="border border-primary border-4 opacity-75 mb-5">`
|
||||||
newQuestionHtml += `</div>`;
|
newQuestionHtml += `</div>`;
|
||||||
|
|
||||||
$("#questions-container .form-group").append(newQuestionHtml);
|
$("#questions-container .form-group").append(newQuestionHtml);
|
||||||
questionIndex++;
|
questionIndex++;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add answer dynamically without "Add Answer" button
|
|
||||||
$("#questions-container").on("click", ".add-answer", function () {
|
$("#questions-container").on("click", ".add-answer", function () {
|
||||||
var questionIndex = $(this).closest('.answers-container').data('question-index');
|
var questionIndex = $(this).closest('.answers-container').data('question-index');
|
||||||
var answerIndex = $(this).closest('.answers-container').find('.answer-group').length;
|
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
|
var answerGroupHtml = `
|
||||||
answerGroup.find('.remove-answer').show(); // Show the "Remove Answer" button
|
<div class="answer-group">
|
||||||
|
<br>
|
||||||
|
<label class="control-label">Answer ${answerIndex + 1}</label>
|
||||||
|
<input type="text" class="form-control" name="Questions[${questionIndex}].Answers[${answerIndex}].Text" placeholder="new answer"/>
|
||||||
|
<button type="button" class="btn btn-danger remove-answer"><i class="bi bi-trash3-fill"></i></button>
|
||||||
|
<br>
|
||||||
|
|
||||||
answerGroup.data('answer-index', answerIndex);
|
</div>`;
|
||||||
answerGroup.find('label').text(`Answer ${answerIndex + 1}`);
|
|
||||||
answerGroup.find('input').attr('name', `Questions[${questionIndex}].Answers[${answerIndex}].Text`);
|
// Append the new answer group before the "Add Answer" button
|
||||||
$(this).closest('.answers-container').append(answerGroup);
|
$(this).before(answerGroupHtml);
|
||||||
|
|
||||||
|
// Show the "Remove Answer" button only for the newly added answer group
|
||||||
|
$(this).prev('.answer-group').find('.remove-answer').show();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Remove answer dynamically
|
// Remove answer dynamically
|
||||||
$("#questions-container").on("click", ".remove-answer", function () {
|
$("#questions-container").on("click", ".remove-answer", function () {
|
||||||
$(this).closest('.answer-group').remove();
|
$(this).closest('.answer-group').remove();
|
||||||
|
|
|
||||||
103
Web/Areas/Admin/Views/Questionnaire/Details.cshtml
Normal file
103
Web/Areas/Admin/Views/Questionnaire/Details.cshtml
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
@model QuestionnaireViewModel
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Details";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="container-fluid d-flex justify-content-center">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="card ">
|
||||||
|
<div class="card-header">
|
||||||
|
Details
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card-body shadow rounded ">
|
||||||
|
<div class="card-title">
|
||||||
|
|
||||||
|
<h5 class="font-weight-bold">Details of questionnaire <span class="badge bg-light p-2 rounded">@Model.Title</span></h5>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<table class="table table-bordered table-responsive table-hover ">
|
||||||
|
<thead>
|
||||||
|
<tr >
|
||||||
|
<th scope="col" class="text-primary h5">Id</th>
|
||||||
|
<th scope="col" class="text-primary h5">Questionnaire</th>
|
||||||
|
<th scope="col" class="text-primary h5">Description</th>
|
||||||
|
<th scope="col" class="text-success h5 ">Questions</th>
|
||||||
|
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><span class="badge p-2 m-1 bg-primary shadow-sm rounded">@Model.Id</span></th>
|
||||||
|
<th scope="row"><span class="badge p-2 m-1 bg-primary shadow-sm">@Model.Title</span></th>
|
||||||
|
<th scope="row"><span class="badge p-2 m-1 bg-primary shadow-sm">@Html.Raw(@Model.Description)</span></th>
|
||||||
|
|
||||||
|
<td class="h6">
|
||||||
|
<table>
|
||||||
|
<tr >
|
||||||
|
<th class="text-success">Id</th>
|
||||||
|
<th class="text-success">Question</th>
|
||||||
|
<th class="text-success">Question Type</th>
|
||||||
|
<th class="text-info">Answers</th>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
@foreach (var question in Model.Questions)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td> <span class="badge p-2 m-1 bg-success ">@question.Id</span></td>
|
||||||
|
<td>
|
||||||
|
<span class="badge p-2 m-1 bg-success ">@question.Text</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span class="badge p-2 m-1 bg-success ">@question.Type</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<table class="table-borderless">
|
||||||
|
<tr>
|
||||||
|
<th class="text-info">Id</th>
|
||||||
|
<th class="text-info">Answer</th>
|
||||||
|
</tr>
|
||||||
|
@foreach (var answer in question.Answers)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<span class="badge p-2 m-1 bg-info shadow-sm">@answer.Id</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span class="badge p-2 m-1 bg-info shadow-sm">@answer.Text</span>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
<footer>
|
||||||
|
|
||||||
|
<a class="btn btn-primary" asp-action="Index">Back to List</a>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
@ -10,58 +10,62 @@
|
||||||
<h5 class="card-title">Edit Survey</h5>
|
<h5 class="card-title">Edit Survey</h5>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<form asp-action="Edit">
|
<form asp-action="Edit">
|
||||||
<input type="hidden" asp-for="Id" />
|
<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>
|
|
||||||
|
|
||||||
<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">
|
<div class="form-group">
|
||||||
<label asp-for="Questions[i].Answers[j].Text" class="control-label"></label>
|
<label asp-for="Title" class="control-label"></label>
|
||||||
<input asp-for="Questions[i].Answers[j].Text" class="form-control" />
|
<input asp-for="Title" class="form-control" />
|
||||||
<span asp-validation-for="Questions[i].Answers[j].Text" class="text-danger"></span>
|
<span asp-validation-for="Title" class="text-danger"></span>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" class="btn btn-danger removeAnswer">Remove Answer</button>
|
<div class="form-group">
|
||||||
</div>
|
<label asp-for="Description" class="control-label"></label>
|
||||||
}
|
<textarea asp-for="Description" class="form-control"></textarea>
|
||||||
</div>
|
<span asp-validation-for="Description" class="text-danger"></span>
|
||||||
<button type="button" class="btn btn-primary addAnswer">Add Answer</button>
|
</div>
|
||||||
<button type="button" class="btn btn-danger removeQuestion">Remove Question</button>
|
<div class="card shadow rounded">
|
||||||
</div>
|
<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>
|
||||||
|
<button type="button" class="btn btn-danger removeAnswer">Remove Answer</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<button type="button" class="btn btn-primary addAnswer">Add Answer</button>
|
||||||
|
|
||||||
<button type="button" class="btn btn-primary" id="addQuestion">Add Question</button>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary">Update</button>
|
}
|
||||||
</form>
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">Update</button>
|
||||||
|
| <a asp-action="Index" class="btn btn-info">Back to list</a>
|
||||||
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -72,34 +76,46 @@
|
||||||
|
|
||||||
|
|
||||||
@section Scripts {
|
@section Scripts {
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function () {
|
CKEDITOR.replace("Description");
|
||||||
// Function to add a new answer
|
</script>
|
||||||
$(document).on('click', '.addAnswer', function () {
|
@{
|
||||||
var questionContainer = $(this).closest('.question');
|
<partial name="_ValidationScriptsPartial" />
|
||||||
var newQuestionIndex = questionContainer.index();
|
}
|
||||||
|
|
||||||
// Correctly find the length of answers for the specific question
|
<script>
|
||||||
var newAnswerIndex = questionContainer.find('.answers .form-group').length;
|
$(document).ready(function () {
|
||||||
|
// Function to add a new answer
|
||||||
|
$(document).on('click', '.addAnswer', function () {
|
||||||
|
var questionContainer = $(this).closest('.question');
|
||||||
|
var newQuestionIndex = questionContainer.index();
|
||||||
|
|
||||||
var answerHtml = `
|
// Correctly find the length of answers for the specific question
|
||||||
<div class="form-group">
|
var newAnswerIndex = questionContainer.find('.answers .form-group').length;
|
||||||
<label class="control-label">Answer Text</label>
|
|
||||||
<input type="text" name="Questions[${newQuestionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" />
|
|
||||||
</div>`;
|
|
||||||
|
|
||||||
// Append the new answer to the specific question
|
var answerHtml = `
|
||||||
questionContainer.find('.answers').append(answerHtml);
|
<div class="form-group">
|
||||||
|
<label class="control-label">Answer</label>
|
||||||
|
<input type="text" name="Questions[${newQuestionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" />
|
||||||
|
<button type="button" class="btn btn-danger removeAnswer">Remove Answer</button>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
// Append the new answer to the specific question
|
||||||
|
questionContainer.find('.answers').append(answerHtml);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Function to remove an answer (including dynamically added answers)
|
||||||
|
$(document).on('click', '.removeAnswer', function () {
|
||||||
|
$(this).closest('.form-group').remove();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
// 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 *@
|
|
||||||
|
|
||||||
|
@* this script is working for adding mutiple answers for a question *@
|
||||||
|
|
||||||
|
|
||||||
@* <script>
|
@* <script>
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
@{
|
@{
|
||||||
ViewData["Title"] = "Questionnaire";
|
ViewData["Title"] = "Questionnaire";
|
||||||
}
|
}
|
||||||
<div class="container-fluid ">
|
<div class="container-fluid d-flex justify-content-center">
|
||||||
|
|
||||||
<partial name="_Notification" />
|
<partial name="_Notification" />
|
||||||
|
|
||||||
|
|
@ -14,14 +14,14 @@
|
||||||
<p>
|
<p>
|
||||||
<a asp-action="Create" class="btn btn-primary"><span><i class="bi bi-plus-square-fill"></i></span> 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>
|
</p>
|
||||||
<table class="table table-responsive d-block table-light table-hover">
|
<table class="table table-responsive table-light table-hover ">
|
||||||
<thead class="w-auto">
|
<thead class="w-auto">
|
||||||
<tr>
|
<tr>
|
||||||
|
|
||||||
<th scope="col">Id</th>
|
<th scope="col">Id</th>
|
||||||
<th scope="col">Title</th>
|
<th scope="col">Title</th>
|
||||||
<th scope="col">Description</th>
|
<th scope="col">Description</th>
|
||||||
<th scope="col">Total Question</th>
|
<th scope="col">Total Questions</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"> <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>
|
<th scope="col" class="d-flex justify-content-end">Action</th>
|
||||||
|
|
@ -63,10 +63,10 @@
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
|
|
||||||
<td class="d-flex justify-content-end">
|
<td class="d-flex justify-content-end">
|
||||||
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-s"><i class="bi bi-trash"></i> Delete</a> |
|
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-s"><i class="bi bi-trash"></i> Delete</a> |
|
||||||
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-warning btn-s"><i class="bi bi-pencil-square"></i> Edit</a>
|
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-success btn-s"><i class="bi bi-pencil-square"></i> Edit</a>|
|
||||||
|
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-info btn-s"><i class="bi bi-pencil-square"></i> Details</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
@ -16,7 +17,7 @@
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.1" />
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue