我有 2 个模型 - 问题和类别 -
public class Question
{
[ScaffoldColumn(false)]
public int QuestionId { get; set; }
[Required]
public string QuestionText { get; set; }
[Required]
public string AnswerA { get; set; }
[Required]
public string AnswerB { get; set; }
[Required]
public string AnswerC { get; set; }
[Required]
public string AnswerD { get; set; }
[Required]
public int Correct { get; set; }
[ForeignKey("Category")]
[Display(Name = "Category")]
[Required]
public int categoryId;
//Navigation property
public virtual Category Category { get; set; }
}
public class Category
{
[ScaffoldColumn(false)]
public int CategoryId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Question> Question { get; set; }
}
在我的 QuestionController 中,我添加了代码以便能够访问视图中下拉列表的可用类别 -
private void PopulateCategoryDropDownList(object selectedCategory = null)
{
var categoryQuery = from c in db.Categories
orderby c.Name
select c;
ViewBag.categoryId = new SelectList(categoryQuery, "CategoryId", "Name", selectedCategory);
}
我有以下创建方法 -
// GET: /Question/Create
public ActionResult Create()
{
PopulateCategoryDropDownList();
return View();
}
//
// POST: /Question/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Question question)
{
try
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
if (ModelState.IsValid)
{
db.Questions.Add(question);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException dex)
{
ModelState.AddModelError("",dex.Message);
}
PopulateCategoryDropDownList(question.Category.CategoryId);
return View(question);
}
我对创建新问题的看法如下 - @model Quiz.Models.Question
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Question</legend>
<div class="editor-label">
@Html.LabelFor(model => model.QuestionText)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.QuestionText)
@Html.ValidationMessageFor(model => model.QuestionText)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AnswerA)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AnswerA)
@Html.ValidationMessageFor(model => model.AnswerA)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AnswerB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AnswerB)
@Html.ValidationMessageFor(model => model.AnswerB)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AnswerC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AnswerC)
@Html.ValidationMessageFor(model => model.AnswerC)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AnswerD)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AnswerD)
@Html.ValidationMessageFor(model => model.AnswerD)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Correct)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Correct)
@Html.ValidationMessageFor(model => model.Correct)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.categoryId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.categoryId,(SelectList)ViewBag.categoryId)
@Html.ValidationMessageFor(model => model.categoryId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
所以,问题是虽然可以创建问题,但下拉列表中的 categoryId 始终为空。
我尝试了很多事情,从尝试直接访问下拉列表到创建不同的视图模型。但是,它们都没有按要求工作。此外,我的代码遵循在线可用的教程。我无法弄清楚有什么不同。
请帮我找出我的代码中的错误。