
[img]https://i.sstatic .net/199g4tG3.jpg[/img]
Когда я хочу добавить вопрос в свой проект в разделе проекта, я хочу, чтобы меня перенаправили на страницу questionsFilter.cshtml с идентификатором проекта. . После этого я хочу добавить и сохранить вопросы, на которые я нажимаю кнопки, связанные с этим идентификатором проекта. Между проектами и вопросами существует связь «многие ко многим». Есть ли кто-нибудь, кто может мне помочь с этим? Кроме того, я использую Razor Pages с методами OnPost и OnGet и LINQ.
Сейчас я могу написать только часть нажатия кнопки. Я не мог понять, что писать в разделах onpost и onget.
@section scripts
{
// An array to hold the IDs of the selected questions
let selectedQuestions = [];
function addQuestion(questionId, button) {
// Check the current class of the button
if (button.classList.contains('btn-outline-primary')) {
// Button selected, add ID and make button red
selectedQuestions.push(questionId);
button.classList.remove('btn-outline-primary');
button.classList.add('btn-danger');
console.log('Selected Question ID:', questionId);
} else {
// Button not selected, remove ID and revert button
selectedQuestions = selectedQuestions.filter(id => id !== questionId);
button.classList.remove('btn-danger');
button.classList.add('btn-outline-primary');
console.log('Removed from question, ID:', questionId);
}
// You can perform other operations here if necessary
// To add a hidden input, use the following code:
const form = document.getElementById('questionForm');
let hiddenInput = document.querySelector(`input[name='SelectedQuestionIds'][value='${questionId}']`);
if (hiddenInput) {
form.removeChild(hiddenInput); // Remove if exists
} else {
hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = 'SelectedQuestionIds';
hiddenInput.value = questionId;
form.appendChild(hiddenInput); // Add new hidden input
}
}
}
Questions
Select Questions
@if (!Model.questions.Any())
{
No Questions Added Yet.
}
else
{
@for (int i = 0; i < Math.Min(20, Model.questions.Count()); i++)
{
var q = Model.questions;
Add Question
}
}
Add Selected Questions
Подробнее здесь: https://stackoverflow.com/questions/791 ... in-razor-p
Мобильная версия