Asp.NET IEnumerable部分页面中没有ViewData

时间:2019-03-19 17:53:04

标签: c# asp.net asp.net-mvc entity-framework

我在尝试在索引页面中加载部分视图时遇到问题,该网站显示的错误如下:

没有类型为“ IEnumerable”的ViewData项目具有键“ 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" }))

我的代码怎么了?

2 个答案:

答案 0 :(得分:0)

发生异常是因为QuestionTypeViewBag属性,并且您没有向视图返回任何模型,还包含潜在的命名冲突,因为QuestionType已经作为viewmodel属性存在:

ViewBag.QuestionType = new SelectList(db.QuestionTypes, "Id", "Name");
return View();

使用DropDownList助手时,您没有指定选项列表:

@Html.DropDownList("QuestionType", null, htmlAttributes: new { @class = "form-control" })

向下拉列表中提供选项列表的推荐方法是通过创建强类型的viewmodel属性来保存选项列表,如下例所示:

public class MyQuestion
{
    // other properties

    // this property bounds to dropdownlist
    public int QuestionType { get; set; }

    // option list property
    public List<SelectListItem> QuestionTypeList { get; set; }
}

然后,修改控制器操作和局部视图以保留所选值:

控制器

public ActionResult Create()
{
    var myQuestion = new MyQuestion();

    myQuestion.QuestionTypeList = db.QuestionTypes.Select(x => new SelectListItem 
    {
        Text = x.Name, 
        Value = x.Id 
    }).ToList();
    return View(myQuestion);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyQuestion mq)
{
    if (ModelState.IsValid)
    {
        // process data and redirect
    }

    mq.QuestionTypeList = db.QuestionTypes.Select(x => new SelectListItem 
    { 
        Text = x.Name, 
        Value = x.Id, 
        Selected = (x.Id == mq.QuestionType) 
    }).ToList();
    return View(mq);
}

局部视图

<div class="form-group">
    @Html.LabelFor(model => model.QuestionType, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.QuestionType, Model.QuestionTypeList, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.QuestionType, "", new { @class = "text-danger" })
    </div>
</div>

答案 1 :(得分:0)

只需编写您的@Html.DropDownList,如下所示:

@Html.DropDownList("QuestionType", ViewBag.QuestionType as SelectList,"Select Question Type", htmlAttributes: new { @class = "form-control" })