我正在尝试使用 MVC3 razor 和 EF4 开发一个小型 Questionbank 应用程序。这是我的 Questionbank 剃须刀页面:
@using System.Linq
@model MvcApplication1.ViewModels.QuestionBankViewModel
@{
ViewBag.Title = " Question Bank";
}
@Html.ValidationSummary(true)
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
@using (Ajax.BeginForm(new AjaxOptions()
{
UpdateTargetId = "divToAddQuestion",
InsertionMode = InsertionMode.InsertAfter,
}))
{
<div class="div_question">
<table class="table_question">
<tr>
<td rowspan="3">
</td>
<td>
<span>Question Bank Title</span>
</td>
<td>@Html.EditorFor(model => model.QuestionBank.QuestionBankName) @*Question*@
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<div class="div_answer">
<table class="table_answer">
<tr>
<td>
<span>Questions()</span>
</td>
<td>
</td>
<td>
<input type="submit" id="btnAddQuestion" name="ButtonCommand" value="Add Question" class="cancel" />
</td>
<td></td>
</tr>
</table>
</div>
<div id="divToAddQuestion">
</div>
<div id="divToAddAnswer">
</div>
</div>
<div>
<input type="submit" id="btnSaveQuestionBank" name="ButtonCommand" value="Save" />
</div>
}
当我单击 btnAddQuestion 时,我正在调用我的 PartialView _Question 每次我 clcik btnAddQuestion 它都会加载我的 partialView 页面 _Question
这是我的部分观点_问题
@using System.Linq
@model MvcApplication1.Models.Question
@{
ViewBag.Title = " Question";
}
@Html.ValidationSummary(true)
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<div class="div_question">
<table class="table_question">
<tr>
<td>
<span>Question Number:</span>
</td>
<td >
@Html.TextBoxFor(model => model.QuestionIndex, new { @class = "tinytextbox" })
</td>
<td></td>
</tr>
<tr>
<td>
<span>Question</span>
</td>
<td>@Html.EditorFor(model => model.strQuestion) @*Question*@
</td>
<td>
@Html.ActionLink("Delete this", "Delete", new { id = Model.QuestionIndex }, new { @class = "deletebutton2", title = "Click to delete this Question and its Answers" })
</td>
</tr>
</table>
<div class="div_answer">
<table class="table_answer">
<tr>
<td>
<span>Answers()</span>
</td>
<td>
</td>
<td>
<input type="submit" id="btnAddAnswer" name="ButtonCommand" value="Add Answer" class="cancel" />
</td>
</tr>
</table>
</div>
</div>
这是我的模型类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class QuestionBank
{
public virtual int QuestionBankId { get; set; }
public virtual string QuestionBankName { get; set; }
public virtual List<Question> Questions { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual int SubjectTypeId { get; set; }
}
public class Question
{
public int QuestionId { get; set; }
public int QuestionIndex { get; set; }
public string strQuestion { get; set; }
}
public class Answer
{
public int AnswerId { get; set; }
public int QuestionId { get; set; }
public string strAnswer { get; set; }
public int AnswerIndex { get; set; }
}
}
这是我的视图模型
using System.Collections.Generic;
using MvcApplication1.Models;
namespace MvcApplication1.ViewModels
{
public class QuestionBankViewModel
{
public QuestionBank QuestionBank { get; set; }
public IEnumerable<Question> Questions { get; set; }
public IEnumerable<Answer> Answers { get; set; }
public string ButtonCommand { get; set; }
public int QuestionIndex { get; set; }
public int AnswerIndex { get; set; }
}
}
当我点击时btnAnswer
,我需要加载我的partialView _Answer
.
@using System.Linq
@model MvcApplication1.Models.Answer
@{
ViewBag.Title = " Answer";
}
@Html.ValidationSummary(true)
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<div class="div_answer">
<table class="table_answer">
<tr>
<td>
<span>Answer Number:</span>
</td>
<td>
@Html.TextBoxFor(model => model.AnswerIndex, new { @class = "tinytextbox" })
</td>
<td>
</td>
</tr>
<tr>
<td>
<span>Answer</span>
</td>
<td>@Html.EditorFor(model => model.strAnswer) @*Question*@
</td>
<td>
</td>
</tr>
</table>
</div>
在这里我可以添加一个答案。每次我点击Add ans
它都会加载新的答案选择。
并且当用户完成添加问题及其对应答案后,用户可以使用 EF4 上下文单击并保存整个问题库。
我的问题是当我点击我的保存按钮时,我无法找到我输入的问题和答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.ViewModels;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
Question qmodel = new Question();
Answer amodel = new Answer();
[HttpGet]
public ActionResult QuestionBank()
{
return View();
}
[HttpPost]
public ActionResult QuestionBank(QuestionBankViewModel model)
{
switch (model.ButtonCommand)
{
case "Add Question":
model.QuestionIndex++;
qmodel.QuestionIndex = model.QuestionIndex ;
return PartialView("_Question", qmodel);
break;
case "Add Answer":
model.AnswerIndex++;
amodel.AnswerIndex = model.AnswerIndex;
return PartialView("_Answer", amodel);
break;
case "Save":
this.SaveQuestionBank(model);
break;
}
return View(model);
}
public ActionResult Question()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public void SaveQuestionBank(QuestionBankViewModel model)
{
QBContext context = new QBContext();
foreach (Question question in model.Questions)
{
context.Question.Add(question);
}
foreach (Answer answer in model.QuestionBank.Answers)
{
context.Answer.Add(answer);
}
context.QuestionBank.Add(model.QuestionBank);
}
public ActionResult About()
{
return View();
}
}
}
我的用户应该能够根据需要添加和删除尽可能多的问题和答案选项,最后他们应该能够通过单击保存按钮来保存所有内容。
当我单击保存按钮时,我期待IEnumerable<Question> Questions
并且IEnumerable<Answer> Answers
在我的控制器中,但我总是将它们设为空。为什么,我该如何解决这个问题?或者你认为我的方法有什么问题吗?
这是我的完整来源,但无法获得我的控制器的问题和答案。感谢您的意见和建议。