本文介绍了传递到字典中的模型项是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);
}

推荐答案

[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类型的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:40