我正在使用 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();
}
}
}