【问题标题】:There is no ViewData in Partial Page Asp.NET IEnumerable部分页面 Asp.NET IEnumerable 中没有 ViewData
【发布时间】:2019-03-19 17:53:04
【问题描述】:

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

没有具有键“QuestionType”的“IEnumerable”类型的 ViewData 项基本上问题似乎出在下拉 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" }))

我的代码有什么问题?

【问题讨论】:

  • 您正试图从模型中获取 QuestionType,但它在视图包中(这是一种不好的做法,您应该立即放弃,而支持通过 ViewModel 进行正确的模型绑定)
  • 这段代码基本上是从Visual Studio自动生成的。
  • 但这基本上是错误的,不是吗? ;)
  • 是的,我该怎么办?那么,我的代码应该是什么样子?

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


【解决方案1】:

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

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

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

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

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

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>

【讨论】:

    【解决方案2】:

    只需将您的@Html.DropDownList 写成如下:

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

    【讨论】:

    • 同样的结果。 :(
    • 问题是您从控制器方法传递的视图数据在部分视图中不可用。
    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 2017-07-15
    • 2021-12-23
    • 2019-05-16
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多