有型无ViewData的项目'的IEnumerable< SelectListItem>'具有键'GradingId“ [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'GradingId'

查看:150
本文介绍了有型无ViewData的项目'的IEnumerable< SelectListItem>'具有键'GradingId“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去得到一个下拉列表,为谁被分级的用户。每个用户可以拥有多个放坡。所以,当我创建一个新的档次,我想一个下拉来指定谁将会接受该级使用。

我不断收到:


  

有类型是没有ViewData的项目'的IEnumerable具有关键GradingId。


我已经看了许多其他问题,但我不能工作了,我需要在我的控制器,视图或模型改变。

GradingController.cs

 公众的ActionResult的Create()
        {
            返回查看();
        }        // POST:放坡/创建
        [HttpPost]
        [ValidateAntiForgeryToken]
        公众的ActionResult创建([绑定(包括= \"GradingId,Eye,Cheek,Mouth,RestSymmetryTotal,RestSymmetryScore,VolForeheadWrinkle,VolGentleEyeClosure,VolOpenMouthSmile,VolSnarl,VolLipPucker,VolSymmetryTotal,VolSymmetryScore,SynForeheadWrinkle,SynGentleEyeClosure,SynOpenMouthSmile,SynSnarl,SynLipPucker,SynkinesisScore,CompositeScore\")]分级分级)
        {
            如果(ModelState.IsValid)
            {
                grading.GradeDate = DateTime.Now;
                db.Gradings.Add(分级);
                db.SaveChanges();
                返回RedirectToAction(「指数」);
            }
            ViewBag.GradingId =新的SelectList(db.Gradings,GradingId,codeNAME);
            返回查看(分级);
        }        // GET:放坡/编辑/ 5
        公众的ActionResult编辑(INT?ID)
        {
            如果(ID == NULL)
            {
                返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
            }
            评分评分= db.Gradings.Find(ID);
            如果(分级== NULL)
            {
                返回HttpNotFound();
            }
            ViewBag.GradingId =新的SelectList(db.Gradings,GradingId,codeNAME);
            返回查看(分级);
        }        // POST:放坡/编辑/ 5
        [HttpPost]
        [ValidateAntiForgeryToken]
        公众的ActionResult编辑([绑定(包括= \"GradingId,Eye,Cheek,Mouth,RestSymmetryTotal,RestSymmetryScore,VolForeheadWrinkle,VolGentleEyeClosure,VolOpenMouthSmile,VolSnarl,VolLipPucker,VolSymmetryTotal,VolSymmetryScore,SynForeheadWrinkle,SynGentleEyeClosure,SynOpenMouthSmile,SynSnarl,SynLipPucker,SynkinesisScore,CompositeScore\")]分级分级)
        {
            如果(ModelState.IsValid)
            {
                db.Entry(分级).STATE = EntityState.Modified;
                db.SaveChanges();
                返回RedirectToAction(「指数」);
            }
            ViewBag.GradingId =新的SelectList(db.Gradings,GradingId,codeNAME);
            返回查看(分级);
        }

Create.cshtml(视图)

  @model FaceToFace.Model.Grading    < H2>创建< / H>
    @using(Html.BeginForm())
    {
        @ Html.AntiForgeryToken()        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=方式> model.User codeNAME,用户名)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.DropDownList(GradingId的String.Empty)
        < / DIV>
            < D​​IV CLASS =表单组>
                < D​​IV CLASS =COL-MD-偏移2 COL-MD-10>
                    <输入类型=提交值=创建类=BTN BTN-默认的/>
                < / DIV>
            < / DIV>
        < / DIV>
    }

grading.cs(模型)

 命名空间FaceToFace.Model
{
    使用系统;
    使用System.Collections.Generic;
    使用System.ComponentModel.DataAnnotations;
    使用System.ComponentModel.DataAnnotations.Schema;
    使用System.Data.Entity.Spatial;    [表(等级)]
    公共部分类分级
    {
公众诠释? User_UserID {搞定;组; }
        公共虚拟用户用户{搞定;组; }        [DatabaseGenerated(DatabaseGeneratedOption.None)
        公众诠释GradingId {搞定;组; }        公众的DateTime GradeDate {搞定;组; }
        公众的DateTime GradeEditDate {搞定;组; }
    }
}

User.cs(模型)

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间FaceToFace.Model
{
    公共类用户
    {
      公共虚拟的ICollection<&分级GT; UserGradings {搞定;组; }
    }
}


解决方案

创建让你不设置操作 ViewBag.GradingId 与在查看导致错误选择列表:

 公众的ActionResult的Create()
{
        ViewBag.GradingId =新的SelectList(db.Gradings,GradingId,codeNAME);
        返回查看();
}

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.

I keep getting:

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 (View)

@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 (model)

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 (model)

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; }   
    }
}

解决方案

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

这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有键'GradingId“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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