没有类型为' IEnumerable& lt; SelectListItem'的ViewData项.具有键' GradingId'的键 [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'GradingId'

查看:80
本文介绍了没有类型为' IEnumerable& lt; SelectListItem'的ViewData项.具有键' GradingId'的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个下拉列表,供正在评分的用户使用.每个用户可以有多个等级.因此,当我创建一个新成绩时,我需要一个下拉列表来指定谁将获得该成绩的使用者.

Im trying to get a drop-down list to work for users who are being graded. Each user can have multiple gradings. So when i create a new grade i want a drop-down to specify which use who will be receiving the grade.

我不断得到:

没有类型为"IEnumerable"的ViewData项目具有键"GradingId".

There is no ViewData item of type 'IEnumerable' that has the key 'GradingId'.

我看了很多其他问题,但是我无法弄清我需要在控制器,视图或模型中进行哪些更改.

I've looked at many other questions but i cant work out what i need to change in my controller, view or models.

GradingController.cs

 public ActionResult Create()
        {
            return View();
        }

        // POST: Gradings/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "GradingId,Eye,Cheek,Mouth,RestSymmetryTotal,RestSymmetryScore,VolForeheadWrinkle,VolGentleEyeClosure,VolOpenMouthSmile,VolSnarl,VolLipPucker,VolSymmetryTotal,VolSymmetryScore,SynForeheadWrinkle,SynGentleEyeClosure,SynOpenMouthSmile,SynSnarl,SynLipPucker,SynkinesisScore,CompositeScore")] Grading grading)
        {
            if (ModelState.IsValid)
            {
                grading.GradeDate = DateTime.Now;
                db.Gradings.Add(grading);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GradingId = new SelectList(db.Gradings, "GradingId", "CodeName");
            return View(grading);
        }

        // GET: Gradings/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Grading grading = db.Gradings.Find(id);
            if (grading == null)
            {
                return HttpNotFound();
            }
            ViewBag.GradingId = new SelectList(db.Gradings, "GradingId", "CodeName");
            return View(grading);
        }

        // POST: Gradings/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "GradingId,Eye,Cheek,Mouth,RestSymmetryTotal,RestSymmetryScore,VolForeheadWrinkle,VolGentleEyeClosure,VolOpenMouthSmile,VolSnarl,VolLipPucker,VolSymmetryTotal,VolSymmetryScore,SynForeheadWrinkle,SynGentleEyeClosure,SynOpenMouthSmile,SynSnarl,SynLipPucker,SynkinesisScore,CompositeScore")] Grading grading)
        {
            if (ModelState.IsValid)
            {
                db.Entry(grading).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GradingId = new SelectList(db.Gradings, "GradingId", "CodeName");
            return View(grading);
        }

Create.cshtml(查看)

@model FaceToFace.Model.Grading

    <h2>Create</h2>


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="editor-label">
            @Html.LabelFor(model => model.User.CodeName, "User Name")
        </div>
        <div class="editor-field">
            @Html.DropDownList("GradingId", String.Empty)
        </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

grading.cs(模型)

namespace FaceToFace.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Grading")]
    public partial class Grading
    {
public int? User_UserID { get; set; }
        public virtual User User { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int GradingId { get; set; }

        public DateTime GradeDate { get; set; }
        public DateTime GradeEditDate { get; set; }
    }
}

User.cs(模型)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FaceToFace.Model
{
    public class User
    {
      public virtual ICollection<Grading> UserGradings { get; set; }   
    }
}

推荐答案

Create 操作中,您未使用SelectList设置 ViewBag.GradingId ,这会导致错误查看:

In Create get action you are not setting ViewBag.GradingId with the SelectList which is causing error in View:

public ActionResult Create()
{
        ViewBag.GradingId = new SelectList(db.Gradings, "GradingId", "CodeName");
        return View();
}

这篇关于没有类型为&amp;#39; IEnumerable&amp; lt; SelectListItem&amp;#39;的ViewData项.具有键&amp;#39; GradingId&amp;#39;的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆