0

我有 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 始终为空。

在此处输入图像描述

第一张图是创建页面,第二张是断点上的create方法

我尝试了很多事情,从尝试直接访问下拉列表到创建不同的视图模型。但是,它们都没有按要求工作。此外,我的代码遵循在线可用的教程。我无法弄清楚有什么不同。

请帮我找出我的代码中的错误。

4

1 回答 1

0

我们可能不得不缩小范围,我感觉从 ViewBag 中正确读取的模型出了点问题。尝试将您的Create操作替换为以下内容,其中您的自定义 ViewBag 填充功能已被删除:

public ActionResult Create() {
        ViewBag.categoryId = new SelectList(db.Categories, "CategoryId", "Name");
        return View();
}

[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);
    }
    ViewBag.categoryId = new SelectList(db.Categories, "CategoryId", "Name", question.Category.CategoryId);
    return View(question);
}

这运行正确吗?

如果这不起作用,那一定是模型绑定问题。我能想到的最后一件事是更改 ViewBag 调用以影响字段Category而不是CategoryId. 在制作 DropDownList 时还要更新您的视图。

于 2013-08-08T05:16:41.073 回答