首页 > 解决方案 > 没有“IEnumerable”类型的 ViewData 项'那有关键的研究'

问题描述

我在另一个表中添加了一个 studid 参考

当我存储 studid 然后给出错误没有具有密钥 studid 的“IEnumerable”类型的 ViewData 项

家庭控制器.cs


        private readonly dbstuudEntities dbstud;

        public HomeController()
        {
            dbstud = new dbstuudEntities();
        }

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

        [HttpPost]
        public ActionResult BlogCreate(blog blg)
        {

                ViewBag.studid = new SelectList(dbstud.students, "studid");
                dbstud.blogs.Add(blg);
                dbstud.SaveChanges();
        }

BlogCreate.cshtml

@model DemoFFI.Models.blog

@{
    ViewBag.Title = "BlogCreate";

}

<h2>BlogCreate</h2>

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

    <div class="form-horizontal">
        <h4>blog</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @*<div class="form-group">
            @Html.LabelFor(model => model.blogid, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.blogid, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.blogid, "", new { @class = "text-danger" })
            </div>
        </div>*@

        <div class="form-group">
            @Html.LabelFor(model => model.blogdescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.blogdescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.blogdescription, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.blogtype, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(x => x.blogtype, new List<SelectListItem>
    {
                        new SelectListItem() {Text = "public", Value="public"},
                        new SelectListItem() {Text = "private", Value="private"},
    })

                @*@Html.EditorFor(model => model.blogtype, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.ValidationMessageFor(model => model.blogtype, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.studid, "studid", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">

                 //here get an error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key studid'

                @Html.DropDownListFor(model => model.studid, ViewBag.studid as IEnumerable<SelectListItem>)

                @Html.ValidationMessageFor(model => model.studid, "", new { @class = "text-danger" })
            </div>
        </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>
}

https://i.stack.imgur.com/CMF0P.png

我在另一个表中添加了一个 studid 引用,但它存储了 null

在此处输入图像描述

//这里得到一个错误没有'IEnumerable'类型的ViewData项具有密钥stuid'

@Html.DropDownListFor(model => model.studid, ViewBag.studid as IEnumerable<SelectListItem>)

标签: c#asp.net-mvcviewbag

解决方案


推荐阅读