传递到字典中的模型项是WebSite.Class'类型,但此字典需要WebSite.Models.Class类型的模型项 [英] The model item passed into the dictionary is of type WebSite.Class', but this dictionary requires a model item of type WebSite.Models.Class

查看:75
本文介绍了传递到字典中的模型项是WebSite.Class'类型,但此字典需要WebSite.Models.Class类型的模型项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我编辑错误时,更新记录...



传入字典的模型项目类型为'IkkosAdminPortalWebSite.RecordDetail',但是这个字典需要一个'IkkosAdminPortalWebSite.Models.RecordDetails'类型的模型项。



在这个RecordDetails --- ModelClass,RecordDetail --- .edmx中的类--.edmx.tt





以下是我的代码..



[HttpGet]

public ActionResult Edit(string actor = null)

{

RecordDetails record = objList.RecordDetails.Find(actor) as RecordDetails;

// var record = objList.RecordDetails.Find(actor);

if(record == null)

{

返回HttpNotFound();

}

返回查看(记录);

//返回View();

}



[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Edit(RecordDetails EditRecord)

{



if(ModelState.IsValid)

{

objList.Entry(EditRecord).State = System.Data.EntityState.Modified;

objList.SaveChanges();

返回RedirectToAction(RecordDetails);

}



return View(EditRecord);

}

Following Is my Error while i Edit, Update the record...

"The model item passed into the dictionary is of type 'IkkosAdminPortalWebSite.RecordDetail', but this dictionary requires a model item of type 'IkkosAdminPortalWebSite.Models.RecordDetails'."

In this RecordDetails---ModelClass, RecordDetail---Class in .edmx--.edmx.tt


Following is my code..

[HttpGet]
public ActionResult Edit(string actor= null)
{
RecordDetails record = objList.RecordDetails.Find(actor) as RecordDetails;
//var record = objList.RecordDetails.Find(actor);
if (record == null)
{
return HttpNotFound();
}
return View(record);
// return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(RecordDetails EditRecord)
{

if (ModelState.IsValid)
{
objList.Entry(EditRecord).State = System.Data.EntityState.Modified;
objList.SaveChanges();
return RedirectToAction("RecordDetails");
}

return View(EditRecord);
}

推荐答案

你应该返回类型 IkkosAdminPortalWebSite.Models.RecordDetails 因为视图需要模型@model IkkosAdminPortalWebSite.Models.RecordDetails ,但您在编辑控制器中返回类型 IkkosAdminPortalWebSite.RecordDetail 。因此,在返回类型之前,将 IkkosAdminPortalWebSite.RecordDetail 的所有属性值设置为 IkkosAdminPortalWebSite.Models.RecordDetails ,如下所示



You should return the type IkkosAdminPortalWebSite.Models.RecordDetails because the view is expecting model @model IkkosAdminPortalWebSite.Models.RecordDetails but you are returning the type IkkosAdminPortalWebSite.RecordDetail in the edit controller. So before you return the type set all the properties values of IkkosAdminPortalWebSite.RecordDetail to IkkosAdminPortalWebSite.Models.RecordDetails like below

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(RecordDetails EditRecord)
{

if (ModelState.IsValid)
{
objList.Entry(EditRecord).State = System.Data.EntityState.Modified;
objList.SaveChanges();
return RedirectToAction("RecordDetails");
}
//Change you code little bit like below
 var viewModel=new  IkkosAdminPortalWebSite.Models.RecordDetails();
view.Property1=EditRecord.Property1;
//set all the properties you want
return View(viewModel);
}





希望这有帮助



Hope this helps


这篇关于传递到字典中的模型项是WebSite.Class'类型,但此字典需要WebSite.Models.Class类型的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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