C# 传递到字典中的模型项的类型为错误
我在一个页面上使用多个模型,但根据指定的id检索数据时出错。错误(传递到字典中的模型项的类型为“Proj.model.PersonInfo”,但此字典需要类型为“Proj.model.Person”的模型项。)C# 传递到字典中的模型项的类型为错误,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我在一个页面上使用多个模型,但根据指定的id检索数据时出错。错误(传递到字典中的模型项的类型为“Proj.model.PersonInfo”,但此字典需要类型为“Proj.model.Person”的模型项。) 公共类PersonInfo { 公共int PersonId{get;set;} 公共字符串名{get;set;} 公共字符串Lastname{get;set;} } 公共类联系人信息 { 公共int PersonId{get;set;} 公共字符串地址{get;set;} 公用字符串电
公共类PersonInfo
{
公共int PersonId{get;set;}
公共字符串名{get;set;}
公共字符串Lastname{get;set;}
}
公共类联系人信息
{
公共int PersonId{get;set;}
公共字符串地址{get;set;}
公用字符串电话{get;set;}
}
公共阶层人士
{
公共PersonInfo PersonInfo{get;set;}
公共联系人信息联系人信息{get;set;}
}
公共行动结果个人信息(int id)
{
返回视图(db.GetPersonInfo().FirstOrDefault(m=>m.PersonId==id));
}
公共列表GetPersonInfo()
{
列表信息=null;
使用(OracleConnection conn=新的OracleConnection(_conn))
{
conn.Open();
string query=“从人员信息中选择*”;
使用(OracleCommand cmd=new OracleCommand(查询,连接))
{
cmd.CommandType=System.Data.CommandType.Text;
OracleDataReader=cmd.ExecuteReader();
if(reader.HasRows)
{
info=新列表();
while(reader.Read())
{
信息添加(新PersonInfo)
{
PersonId=Convert.ToInt32(读卡器[“P_ID”]),
Firstname=读卡器[“FIRST_NAME”],
Lastname=reader[“LAST_NAME”].ToString()
});
}
}
}
}
退货信息;
}
尝试从
public ActionResult PersonInfo(int id)
{
return View(db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id));
}
到
您的问题本身说明了答案,将模型传递给所需的视图凯恩,非常感谢您。。。你解决了我的问题。
public ActionResult PersonInfo(int id)
{
return View(db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id));
}
public ActionResult PersonInfo(int id)
{
var model = new Person { PersonInfo = db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id)}
return View(model);
}