asp.net - 在 asp.net mvc 中传入字典的模型项
问题描述
执行程序时出现错误,错误内容为:传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.Child_A03B4AB53F9E2ED1CB70F81E60FFA68CF868CF01DE6D2C340D267350F8543009”,但该字典需要“adminSection.Models.Note”类型的模型项。
执行此视图时
@model adminSection.Models.Note
@{
ViewBag.Title = "Add_Note";
}
<h2>Add_Note</h2>
@using (Html.BeginForm("Add_Note", "Admin", FormMethod.Post))
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(x=>x.Note_title,htmlAttributes:new { @class="label-control col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.Note_title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(x => x.Note_title, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.Note_title, htmlAttributes: new { @class = "label-control col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(x => x.Note_title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(x => x.Note_title, null, new { @class = "text-danger" })
</div>
</div>
</div>
<div>
<input type="submit" value="Add" class="btn btn-default" />
</div>
}
我的控制器是
[HttpGet]
public ActionResult Add_Note(int? id)
{
if (Session["ID"] == null)
return RedirectToAction("Login", "Home");
else
{
if (id == null)
{
return HttpNotFound();
}
else
{
using (Context db = new Context())
{
var child = db.child.Find(id);
if (child == null)
{
return HttpNotFound();
}
else
{
return View(child);
}
}
}
}
}
[HttpPost]
public ActionResult Add_Note(Note note, Child child)
{
if (ModelState.IsValid)
{
note.Child_id = child.Chi_Id;
note.Note_Date = DateTime.Now;
child.note = new List<Note>();
db.Entry(child).State = EntityState.Modified;
var admin = db.admin.Find(Session["Id"]);
if (admin != null)
{
if (admin.Adm_Name == Session["Name"].ToString())
{
admin.note = new List<Note>();
admin.note.Add(note);
db.Entry(admin).State = EntityState.Modified;
note.admin = admin;
//db.SaveChanges();
}
}
else
{
return HttpNotFound();
}
}
child.note.Add(note);
note.child = child;
db.note.Add(note);
db.SaveChanges();
return RedirectToAction("Home", "Admin");
}
和我的孩子班
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace adminSection.Models
{
[Table("tbl_child")]
public partial class Child
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Chi_Id { get; set; }
/// <summary>
/// child Id att
/// </summary>
[Required(ErrorMessage ="you must provied child first name")]
[Display(Name ="Child First Name")]
public string Chi_First_Name { get; set; }
/// <summary>
/// First name att
/// </summary>
[Required(ErrorMessage ="you must provied child last Name")]
[Display(Name ="Child Last Name")]
public string Chi_Last_Name { get; set; }
/// <summary>
/// Last Name Att
/// </summary>
[Required(ErrorMessage ="you must provied child age")]
[Display(Name ="Child Age")]
[Range(0,18, ErrorMessage = "Correct Input")]
public int Age { get; set; }
/// <summary>
/// Child Age Att
/// </summary>
[Required(ErrorMessage ="you must provied child gender")]
[Display(Name ="Child Gender")]
public string Gender { get; set; }
/// <summary>
/// Child Gender Att
/// </summary>
[Display(
Name ="Child School")]
public string Chi_School { get; set; }
/// <summary>
/// Child School
/// </summary>
[Required(ErrorMessage ="you must provied parent status")]
[Display(Name ="Child Parent Status")]
public string Chi_parent_status { get; set; }
/// <summary>
/// Child Parent Status
/// </summary>
///
[Display(Name ="Child Image Path")]
public string Chi_Image_Path { get; set; }
/// <summary>
/// Child Image Status
/// </summary>
///
public virtual Admin admin { get; set; }
public int Adm_ID { get; set; }
public virtual Parent parent { get; set; }
public int Par_ID { get; set; }
public ICollection<Note> note { get; set; }
}
}
和我的课堂笔记
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace adminSection.Models
{
[Table("tbl_Note")]
public class Note
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[ScaffoldColumn(false)]
public int Note_Id { get; set; }
/// <summary>
/// note id end
/// </summary>
[Required(ErrorMessage ="You must provide this note title")]
[Display(Name ="Header")]
public string Note_title { get; set; }
/// <summary>
/// note of title end
/// </summary>
[Required(ErrorMessage ="You must provide note content")]
[Display(Name = "content")]
public string note_content { get; set; }
/// <summary>
///
///Note Content end
/// </summary>
public DateTime Note_Date { get; set; }
/// <summary>
/// Date Of note here
/// </summary>
public virtual Child child { get; set; }
public int Child_id { get; set; }
// note with Child ont to many
public virtual Admin admin { get; set; }
public int Admin_id { get; set; }
// Note with Admin and note is many to one
public virtual Parent parent { get; set; }
public int Parent_Id { get; set; }
// Note with Parent and note is many to one
}
}
解决方案
我认为使用对象的类型(可能是 Note)而不是var
invar child = db.child.Find(id)
可以解决您的问题。
推荐阅读
- python - 列表索引超出范围 - 在排序列表中查找重复项
- linux - 如何防止cmake剥离创建的共享库
- excel - 如果另一张纸上的值大于/小于任一值,则在 Excel 中突出显示 2 个单元格
- origen-sdk - Origen.load_target 是否允许增加初始化选项?
- spring-boot - Minikube VM hyperkit:Spring Boot:连接到本地机器
- python - 没有输出到文件
- sql - 如何在 SQL Server 的表中创建简单的索引列?
- java - 如何在java中单击下拉切换ng-binding selenium click元素
- java - 更改数据库后出现休眠错误“您的 SQL 语法有错误”
- javascript - 复选框启用/禁用 DIV - Localstorage - Onload 加载状态