Einfügen von Daten in die Datenbank unter Verwendung von AJAX (ASP.NET MVC )
Ich habe Tabelle mit Question1 - Question10
Hier ist table-syntax
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,
Auch ich habe DropDownLists für diese Fragen
Ist hier es sieht aus wie
Brauche ich auf button Klick Daten aus DropdownLists und schreiben Question1-Question10 Zeilen in der Datenbank.
Hier ist Mein Controller
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());
}
Und hier ist die Ansicht
<div class="title2" style="margin-top: 15px; margin-left: 15px; margin-bottom: 15px; padding-top: 10px">
@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;"})
</div>
Ich denke AJAX kann dies tun, aber wie muss ich den code schreiben, oder wo kann ich darüber schreiben, wie man dies tun?
Danke
UPDATE
Danke Prasanna Kumar J für die Antwort
Ich habe noch eine Frage
Schreibe ich-Funktion und versuchen, führen Sie es durch button-Klick
Schreibe ich diesen code in html -
<input id="save" type="button" value="Save" onclick="save();"/>
Und diese in JS
$(document).ready(function () {
$('#save').click(function () {
save();
});
});
Aber Funktion wird nicht ausgeführt, auf die Schaltfläche. Wo ist der Fehler?
Ja Werte aus dropdownLists und der post-Tabelle@PrasannaKumarJ
Das problem ist, Ihr Modell ist nicht verpflichtet, die Felder und das ist, warum die Antworten Balg versuchen, plain html, um Werte abzurufen, die von post-und asp.net statt MVC Razor. Wenn Sie nur Google für ein vollständiges Beispiel auf, wie binden Sie Ihr Modell in der MVC-erhalten Sie eine saubere Lösung.
entfernen Sie den code onclick="speichern();" input-tag
InformationsquelleAutor | 2017-03-21
Du musst angemeldet sein, um einen Kommentar abzugeben.
Versuchen.
Im Client-Seite
Im server-Seite
QuestionBlock question = new QuestionBlock { Question1 = question1, Question2 = question2, Question3 = question3 // … };
ja, es sollte funktionieren
Ich habe noch eine Frage. Ich post aktualisiert
Ich habe Fehler aufmerksam. Nur error-und keine info
korrekte controller-Namen.was ist deine controller-Namen?
InformationsquelleAutor Prasanna Kumar J
Bist du mit ASP.NET MVC, also in Ihrem Fragen-controller, erstellen Sie eine
[HttpPost]
Methode zur Handhabung von Datenbank-updates.Wenn Sie SQL Server verwenden, können Sie die Klassen in der
System.Data.SqlClient
namespaces und werfen Sie einen Blick auf MSDN-Dokumentation mit Beispielen.Wenn Sie MySQL verwenden, können Sie das MySQL .NET-Stecker - Informationen zur Verfügung auf Ihrer Website einschließlich Dokumentation.
In der Datenbank-update-Methode, die Sie verwenden können, eine
UPDATE
oderINSERT
Abfrage zum aktualisieren der Daten in der Datenbank. Zum abrufen der Informationen, haben eine<form method="post" action="your_update_page">
und ein<input type="submit" />
. Dieser post wird die Informationen in die Eingabe-Felder (mit dem gewünschten Namen entspricht Parameter in update-Methode) zugegriffen werden, indem Sie Ihre back-end-controller, um Datenbank-updates, wenn Sie ausgelöst werden.Es gibt viele Wege zu gehen über diese. Razor erzeugen kann die form, wenn nötig und es gibt viel Dokumentation dafür unter msdn.microsoft.com/en-us/library/...
InformationsquelleAutor Nathangrad