C# 如何在一个表格中插入所有问题?
有3张桌子:C# 如何在一个表格中插入所有问题?,c#,asp.net-mvc,C#,Asp.net Mvc,有3张桌子: 问题(问题ID、问题文本) 调查(调查ID、问题ID、用户ID、答案文本、评论) 用户(用户ID、用户名) 如何在视图中为问题表中的所有问题生成表单 例如,我有90个问题,当我回答这张表格5次时,表格中的答案必须有450条记录 控制器: public class SurveysController : Controller { private TESTEntities db = new TESTEntities(); // GET: Surveys pub
public class SurveysController : Controller
{
private TESTEntities db = new TESTEntities();
// GET: Surveys
public ActionResult Index()
{
var surveys = db.Surveys.Include(s => s.Question).Include(s => s.User);
return View(surveys.ToList());
}
// GET: Surveys/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Survey survey = db.Surveys.Find(id);
if (survey == null)
{
return HttpNotFound();
}
return View(survey);
}
// GET: Surveys/Create
public ActionResult Create()
{
ViewBag.QuestionId = new SelectList(db.Questions, "QuesionId", "QuestionText");
ViewBag.UserId = new SelectList(db.Users, "UserId", "Name");
return View();
}
// POST: Surveys/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "SuerveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
{
if (ModelState.IsValid)
{
db.Surveys.Add(survey);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.QuestionId = new SelectList(db.Questions, "QuesionId", "QuestionText", survey.QuestionId);
ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
return View(survey);
}
// GET: Surveys/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Survey survey = db.Surveys.Find(id);
if (survey == null)
{
return HttpNotFound();
}
ViewBag.QuestionId = new SelectList(db.Questions, "QuesionId", "QuestionText", survey.QuestionId);
ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
return View(survey);
}
// POST: Surveys/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "SuerveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
{
if (ModelState.IsValid)
{
db.Entry(survey).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.QuestionId = new SelectList(db.Questions, "QuesionId", "QuestionText", survey.QuestionId);
ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
return View(survey);
}
// GET: Surveys/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Survey survey = db.Surveys.Find(id);
if (survey == null)
{
return HttpNotFound();
}
return View(survey);
}
// POST: Surveys/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Survey survey = db.Surveys.Find(id);
db.Surveys.Remove(survey);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
创建视图:
@model TEST.Models.Survey
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Survey</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
@foreach (var item in ViewBag.QuestionId)
{
<div class="form-group">
@Html.LabelFor(model => model.QuestionId, "QuestionId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(s=>s.QuestionId)
@Html.ValueFor(s =>item)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Answer, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Answer, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Answer, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model TEST.Models.Survey
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
调查
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.UserId,“UserId”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“UserId”,null,htmlAttributes:new{@class=“formcontrol”})
@Html.ValidationMessageFor(model=>model.UserId,“,new{@class=“text danger”})
@foreach(ViewBag.QuestionId中的变量项)
{
@LabelFor(model=>model.QuestionId,“QuestionId”,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.HiddenFor(s=>s.QuestionId)
@Html.ValueFor(s=>item)
@LabelFor(model=>model.Answer,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Answer,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Answer,“,new{@class=“text danger”})
@LabelFor(model=>model.Comment,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Comment,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Comment,“,new{@class=“text danger”})
}
}
@ActionLink(“返回列表”、“索引”)
首先,我想确保我理解您的意图
在创建操作中,您将从问题表中获取所有问题,并将它们作为问题ID添加到视图包中
假设在调试代码时,当您尝试将标签打印到屏幕时,在Viewbag.QuestionId中看到正确的数据
@Html.LabelFor(model => model.QuestionId, "QuestionId", htmlAttributes: new { @class = "control-label col-md-2" })
您正在尝试打印QuestionId的对象。我认为您需要在标签中使用“item”,因为当代码在集合中迭代时,它会发生变化
item.Question
应该用在你想打印问题文本的地方问题是什么?问题是当我想创建时,它为每个问题创建字段数,但不显示问题,也不在此提交。我不明白你为什么在选择列表中传递问题和用户。实体框架生成了这个,在ado.net模型中,我是否应该更改其他内容?