【问题标题】:Why is there an error, "There is no ViewData item of type 'IEnumerable<SelectListItem>'..."为什么会出现错误“没有 'IEnumerable<SelectListItem>' 类型的 ViewData 项...”
【发布时间】:2016-11-07 01:41:42
【问题描述】:

我正在使用 Visual Studio 2012、C# 和 Razor 创建一个 MVC ASP.NET 表单。按下“提交查询”按钮后,出现错误,“没有类型为 'IEnumerable' 且键为 'Age' 的 ViewData 项”,并且它在视图中的这一行:

年龄@Html.DropDownList("年龄")

这里是视图:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>MVC Cheese Survey</title>
</head>
<body>
    <div>

        @using (Html.BeginForm()) {
        <div>First Name @Html.TextBox("FirstName")
             Last Name @Html.TextBox("LastName")
            <br />
            Address @Html.TextBox("Address")
            Address @Html.TextBox("Address")
            City @Html.TextBox("City")
            State @Html.TextBox("State")
            Zip @Html.TextBox("Zip")
            Phone @Html.TextBox("PhoneNumber")
            Email @Html.TextBox("Email")
            Age @Html.DropDownList("Age")

        </div>
        <input type="submit" name="submit" />
        }
        @ViewData["FirstName"]
    </div>
</body>
</html>

这里是控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcCheeseSurvey.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {   
            List<string> AgeList = new List<string>();
            AgeList.Add("0-17");
            AgeList.Add("18-21");
            AgeList.Add("22-25");
            AgeList.Add("26-35");
            AgeList.Add("36+");

            ViewData["Age"] = new SelectList(AgeList);

            return View();
        }
        [HttpPost]
        public ActionResult Index(string FirstName, string LastName)
        {
            ViewData["FirstName"] = FirstName;
            return View();
        }

    }
}

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    Index 操作的两个版本都需要为ViewData 填充“年龄”数据,如下所示:

    public ActionResult Index()
    {   
        List<string> AgeList = new List<string>();
        AgeList.Add("0-17");
        AgeList.Add("18-21");
        AgeList.Add("22-25");
        AgeList.Add("26-35");
        AgeList.Add("36+");
    
        ViewData["Age"] = new SelectList(AgeList);
    
        return View();
    }
    
    [HttpPost]
    public ActionResult Index(string FirstName, string LastName)
    {
        ViewData["FirstName"] = FirstName;
    
        List<string> AgeList = new List<string>();
        AgeList.Add("0-17");
        AgeList.Add("18-21");
        AgeList.Add("22-25");
        AgeList.Add("26-35");
        AgeList.Add("36+");
    
        ViewData["Age"] = new SelectList(AgeList); 
    
        return View();
    }
    

    【讨论】:

    • 谢谢!最后一件事:如何让帖子上的名字和姓氏文本框为空白? (我仍然希望 FirstName 像现在一样在文本框之外发回。)
    【解决方案2】:

    您必须确保在处理 POST 方法并返回相同的视图时填充您的 ViewData。

    public class HomeController : Controller
    {
        private void EnsureAge()
        {
            List<string> AgeList = new List<string>();
            AgeList.Add("0-17");
            AgeList.Add("18-21");
            AgeList.Add("22-25");
            AgeList.Add("26-35");
            AgeList.Add("36+");
    
            ViewData["Age"] = new SelectList(AgeList);
        }
    
        [HttpPost]
        public ActionResult Index(string FirstName, string LastName)
        {
            EnsureAge();
            ViewData["FirstName"] = FirstName;
            return View();
        }
    
    }
    

    这些值作为浏览器的“功能”保留。您可以通过调用将以下行添加到您的视图中的提交位来明确清除它们:

    <input type="submit" 
           name="submit" 
           onclick="document.getElementById('FirstName').value='';document.getElementById('LastName').value='';"/>
    

    【讨论】:

    • 感谢您的跟进。
    • 没问题,我还检查了在输入元素上设置 autocomplete='off' 并返回 205 状态代码(重置内容)而不是 200,但在 IE10 上对我不起作用。不过没有检查其他浏览器。
    【解决方案3】:

    我也遇到过同样的问题。 就我而言,它缺少 ViewBag 项目。

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddAnswer(Answer answer){ 
           // insert to database
           return View();
        }
    

    这应该看起来像

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddAnswer(Answer answer){ 
           // insert to database
           ViewBag.ListOfQuestions = ListOfQuestions;
           return View();
        }
    

    我的查看代码是:

             <div class="col-md-10">
                @Html.DropDownListFor(model => model.QuestionId, ViewBag.ListOfQuestions as SelectList, "--SELECT--")
                @Html.ValidationMessageFor(model => model.QuestionId)
            </div>
    

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多