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 が常に null になることです。

ここに画像の説明を入力

最初の画像は作成ページを示し、2 番目の画像はブレークポイントでの作成メソッドです

ドロップダウンリストに直接アクセスしようとすることから、別のビューモデルを作成することまで、さまざまなことを試しました。ただし、いずれも必要に応じて機能しません。また、私のコードはオンラインで入手できるチュートリアルに従っています。何が違うのか理解できません。

私のコードの間違いを見つけるのを手伝ってください。

4

1 に答える 1