我正在使用Visual Studio 2012,C#和Razor创建MVC ASP.NET表单。在按下“提交查询”按钮后,出现错误,“没有类型'IEnumerable'的ViewData项具有键'Age',”它位于视图中的这一行:
Age @ Html.DropDownList(“Age”)
以下是视图:
@{
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();
}
}
}
答案 0 :(得分:0)
当处理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='';"/>
答案 1 :(得分:0)
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();
}
答案 2 :(得分:0)
我有同样的问题。 在我的情况下,它缺少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>