2

Visual Studio 2012、C#、および Razor を使用して MVC ASP.NET フォームを作成しています。[Submit Query] ボタンを押すと、「There is no ViewData item of type 'IEnumerable' that has the key 'Age'」というエラーが表示され、View の次の行に表示されます。

年齢 @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();
        }

    }
}
4

3 に答える 3

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='';"/>
于 2013-09-20T19:06:38.847 に答える
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();
}
于 2013-09-20T19:10:38.783 に答える