c# - 没有“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>
}
我在另一个表中添加了一个 studid 引用,但它存储了 null
//这里得到一个错误没有'IEnumerable'类型的ViewData项具有密钥stuid'
@Html.DropDownListFor(model => model.studid, ViewBag.studid as IEnumerable<SelectListItem>)
解决方案
推荐阅读
- reactjs - 将箭头函数返回给组件
- sql - 如何连接声明日期变量
- swift - 添加这些 UIButton 扩展功能如何使我的应用程序性能更好?
- javascript - 如何获取此表单的 div id?
- asp.net - 尝试使用 HttpWebRequest 方法将文件从控制器上传到具有 [FromForm] IFormFile 文件参数的 API 操作接收 null
- python - 读入 SQL 文件并使用 Count Vectorizer 获取单词出现次数
- ruby-on-rails - 日期周围的 RSpec Timecop 故障似乎与时区有关
- unix - 在 UNIX 中将字符串合并到从文本文件中提取的行
- python - 使用 Python3.x ,在 nano 中,第一行无法编辑
- jquery - 为什么向 HTML 页面元素添加 CSS 类会破坏 Turbolinks 获取的页面上的滚动?