Anonymous
Вставка данных в базу данных с помощью AJAX (ASP.NET MVC)
Сообщение
Anonymous » 20 окт 2024, 09:04
У меня есть таблица с Вопросом 1 – Вопросом 10
Вот синтаксис таблицы
Код: Выделить всё
CREATE TABLE [dbo].[QuestionBlocks] (
[Block_ID] INT IDENTITY (1, 1) NOT NULL,
[Question1] NVARCHAR (MAX) NULL,
[Question2] NVARCHAR (MAX) NULL,
[Question3] NVARCHAR (MAX) NULL,
[Question4] NVARCHAR (MAX) NULL,
[Question5] NVARCHAR (MAX) NULL,
[Question6] NVARCHAR (MAX) NULL,
[Question7] NVARCHAR (MAX) NULL,
[Question8] NVARCHAR (MAX) NULL,
[Question9] NVARCHAR (MAX) NULL,
[Question10] NVARCHAR (MAX) NULL,
Также у меня есть DropDownLists для этих вопросов.
Вот как это выглядит
Мне нужно нажать кнопку, получить данные из раскрывающихся списков и записать строки Вопрос1-Вопрос10 в База данных.
Вот мой контроллер
Код: Выделить всё
public ActionResult Index()
{
ViewBag.Question1 = new SelectList(db.Questions,"QuestionId","question");
ViewBag.Question2 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question3 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question4 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question5 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question6 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question7 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question8 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question9 = new SelectList(db.Questions, "QuestionId", "question");
ViewBag.Question10 = new SelectList(db.Questions, "QuestionId", "question");
return View(db.Questions.ToList());
}
А вот и вид
Код: Выделить всё
@Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;",placeholder="lol"})
@Html.DropDownList("Question2", null, "Вопрос 2", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question3", null, "Вопрос 3", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question4", null, "Вопрос 4", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question5", null, "Вопрос 5", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question6", null, "Вопрос 6", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question7", null, "Вопрос 7", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question8", null, "Вопрос 8", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question9", null, "Вопрос 9", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
@Html.DropDownList("Question10", null, "Вопрос 10", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"})
Я думаю, что AJAX может это сделать, но как мне нужно написать код или где я могу написать о том, как это сделать?
Спасибо
ОБНОВЛЕНИЕ
Спасибо Прасанне Кумару Дж. за ответ
У меня еще один вопрос
Я пишу функцию и пытаюсь запустить ее нажатием кнопки
Я пишу этот код на html
И это в JS
Код: Выделить всё
$(document).ready(function () {
$('#save').click(function () {
save();
});
});
Но функция не запускается по кнопке. Где ошибка?
Подробнее здесь:
https://stackoverflow.com/questions/429 ... sp-net-mvc
1729404255
Anonymous
У меня есть таблица с Вопросом 1 – Вопросом 10 Вот синтаксис таблицы [code]CREATE TABLE [dbo].[QuestionBlocks] ( [Block_ID] INT IDENTITY (1, 1) NOT NULL, [Question1] NVARCHAR (MAX) NULL, [Question2] NVARCHAR (MAX) NULL, [Question3] NVARCHAR (MAX) NULL, [Question4] NVARCHAR (MAX) NULL, [Question5] NVARCHAR (MAX) NULL, [Question6] NVARCHAR (MAX) NULL, [Question7] NVARCHAR (MAX) NULL, [Question8] NVARCHAR (MAX) NULL, [Question9] NVARCHAR (MAX) NULL, [Question10] NVARCHAR (MAX) NULL, [/code] Также у меня есть DropDownLists для этих вопросов. Вот как это выглядит [img]https://i.sstatic.net/t3T9i.png[/img] [img]https://i.sstatic.net/hc8AI.png[/img] Мне нужно нажать кнопку, получить данные из раскрывающихся списков и записать строки Вопрос1-Вопрос10 в База данных. Вот мой контроллер [code]public ActionResult Index() { ViewBag.Question1 = new SelectList(db.Questions,"QuestionId","question"); ViewBag.Question2 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question3 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question4 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question5 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question6 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question7 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question8 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question9 = new SelectList(db.Questions, "QuestionId", "question"); ViewBag.Question10 = new SelectList(db.Questions, "QuestionId", "question"); return View(db.Questions.ToList()); } [/code] А вот и вид [code] @Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;",placeholder="lol"}) @Html.DropDownList("Question2", null, "Вопрос 2", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question3", null, "Вопрос 3", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question4", null, "Вопрос 4", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question5", null, "Вопрос 5", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question6", null, "Вопрос 6", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question7", null, "Вопрос 7", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question8", null, "Вопрос 8", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question9", null, "Вопрос 9", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) @Html.DropDownList("Question10", null, "Вопрос 10", htmlAttributes: new {@class = "form-control", @style = "height:40px; margin-bottom: 20px;"}) [/code] Я думаю, что AJAX может это сделать, но как мне нужно написать код или где я могу написать о том, как это сделать? Спасибо [b]ОБНОВЛЕНИЕ[/b] Спасибо Прасанне Кумару Дж. за ответ У меня еще один вопрос Я пишу функцию и пытаюсь запустить ее нажатием кнопки Я пишу этот код на html [code] [/code] И это в JS [code]$(document).ready(function () { $('#save').click(function () { save(); }); }); [/code] Но функция не запускается по кнопке. Где ошибка? Подробнее здесь: [url]https://stackoverflow.com/questions/42924318/insert-data-to-database-using-ajax-asp-net-mvc[/url]