2013-12-11 21 views
0

我正在使用多個模型的單個剃刀頁面,但當我根據指定的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

你的問題本身的答案,通過模型來查看哪些是必需的 –

回答

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); 
} 
+0

凱恩,非常感謝喲...你已經解決了我的問題。 – Bombula

相關問題