2014-11-25 1 views
0

있어 3 테이블 :모든 질문을 하나의 양식에 삽입하는 방법은 무엇입니까?

  1. 질문 (QuestionId, QuestionText)
  2. 조사 (SurveyId, QuestionId, 사용자 아이디, AnswerTExt, 코멘트)
  3. 사용 방법 (사용자 아이디, 사용자 이름)

테이블 질문의 모든 질문에 대한보기로 양식을 작성하십시오.

예 나는이 양식을 5 회에 대답 할 때, 표가 응답 (450 개) 기록

있어야합니다 90 개 질문을 가지고

컨트롤러 :

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> 
+0

문제는 무엇인가의 텍스트를 인쇄 할 경우

item.Question 

이 사용되어야한다? –

+0

문제는 내가 만들 때, 각 질문에 대한 필드의 수를 생성하지만, 질문을 보여 나던하고, 이해가 안가 여기 – vasilens

+0

http://postimg.org/image/u3ag9fzb7/를 제출 나던입니다 왜 당신이 선택 목록에 질문과 사용자를 전달하는지. – vasilens

답변

0

먼저 네가하려는 일을 내가 이해했는지 확인해.

만들기 작업에서 모든 질문을 질문 표에서 가져 와서이를보기 백으로 질문 ID로 추가하고 있습니다.

코드를 디버깅 할 때 당신은 당신은 QuestionId의 목적을 인쇄하려고

@Html.LabelFor(model => model.QuestionId, "QuestionId", htmlAttributes: new { @class = "control-label col-md-2" }) 

으로 화면에 라벨을 인쇄 할 때, 당신은 Viewbag.QuestionId에서 올바른 데이터를 볼 수 있다고 가정. 귀하의 코드가 컬렉션을 반복하면서 변경되기 때문에 레이블에 'item'을 사용해야한다고 생각합니다. 당신이 질문

관련 문제