【发布时间】:2017-08-12 22:53:50
【问题描述】:
我有 Question1 - Question10 的表格
这里是表格语法
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
看起来像这样
我需要点击按钮从 DropdownLists 中获取数据并在数据库中写入 Question1-Question10 行。
这是我的控制器
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());
}
这里是视图
<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>
我认为 AJAX 可以做到这一点,但是我需要如何编写代码或者我可以在哪里写关于如何做到这一点?
谢谢
更新
感谢 Prasanna Kumar J 的回答
我还有一个问题
我编写函数并尝试通过单击按钮来运行它 我用html编写这段代码
<input id="save" type="button" value="Save" onclick="save();"/>
这在 JS 中
$(document).ready(function () {
$('#save').click(function () {
save();
});
});
但功能不在按钮上运行。哪里出错了?
【问题讨论】:
-
您只需要发布选定的问题来保存吗?
-
是的,从下拉列表中获取值并发布到 table@PrasannaKumarJ
-
问题是您的模型未绑定到字段,这就是为什么下面的答案尝试使用纯 html 通过 post 和 asp.net 而不是 MVC Razor 检索值的原因。如果您只是 Google 获取有关如何在 MVC 中绑定模型的完整示例,您将获得一个干净的解决方案。
-
删除代码 onclick="save();"在输入标签上
标签: c# jquery asp.net ajax asp.net-mvc