传递到字典中的模型项的类型为error

时间:2013-12-11 13:29:32

标签: c# asp.net-mvc razor

我使用多个模型一个剃刀页面,但是当我按照指定的id.Error检索数据时出现错误(传递到字典中的模型项是'Proj.Model.PersonInfo'类型,但是这个字典需要一个'Proj.Model.Person'类型的模型项。)

public class PersonInfo
{
    public int PersonId{get;set;}
    public string Firstname{get;set;}
    public string Lastname{get;set;}
}

public class ContactInfo
{
    public int PersonId{get;set;}
    public string Address{get;set;}
    public string Telephone{get;set;}
}

public class Person
{
    public PersonInfo PersonInfo{get;set;}
    public ContactInfo ContactInfo{get;set;}
}

public ActionResult PersonInfo(int id)
{
    return View(db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id));
}

public List<PersonInfo> GetPersonInfo()
{
    List<PersonInfo> info = null;
    using (OracleConnection conn = new OracleConnection(_conn))
    {
        conn.Open();
        string query = "SELECT * FROM PERSON_INFO";
        using (OracleCommand cmd = new OracleCommand(query, conn))
        {
            cmd.CommandType = System.Data.CommandType.Text;
            OracleDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                info = new List<PersonInfo>();
                while (reader.Read())
                {
                    info.Add(new PersonInfo
                    {
                        PersonId = Convert.ToInt32(reader["P_ID"]),
                        Firstname = reader["FIRST_NAME"],
                        Lastname = reader["LAST_NAME"].ToString()
                    });
                }
            }
        }
    }
    return info;
}

1 个答案:

答案 0 :(得分:1)

尝试从

更改此内容
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);
}