У меня возникла проблема при попытке загрузить частичное представление на странице индекса, ошибка, отображаемая на сайте, следующая:
Нет элемента ViewData типа 'IEnumerable', который имеетключ 'QuestionType' в принципе проблема, кажется, в выпадающем html.
Вот мой частичный:
<div class="form-group">
@Html.LabelFor(model => model.QuestionType, "QuestionType", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("QuestionType", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.QuestionType, "", new { @class = "text-danger" })
</div>
</div>
Мой контроллер вопросов
public ActionResult Create()
{
ViewBag.QuestionType = new SelectList(db.QuestionTypes, "Id", "Name");
return View();
}
// POST: MyQuestions/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 = "Id,Title,Description,QuestionDate,Tags,QuestionType")] MyQuestion myQuestion)
{
if (ModelState.IsValid)
{
db.MyQuestions.Add(myQuestion);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.QuestionType = new SelectList(db.QuestionTypes, "Id", "Name", myQuestion.QuestionType);
return View(myQuestion);
}
И вот как я называю частичное:
<div class="modal-body">
@Html.Partial("_CreatePartial", new MyWebAppInMVC.Database.MyQuestion())
</div>
В верхней части частичного я использую следующую строку:
@using (Html.BeginForm("Create", "MyQuestions", FormMethod.Post, new { id = "AddModal" }))
Что не так с моим кодом?