1
我來自webforms,我試圖在MVC中複製一個簡單的數據模型。我是.NET和C#新手,所以如果這是一個非常簡單的問題,請原諒。我有「信件」,每個信件都有一個類別和多個收件人。 EF似乎在SQL後端上正確創建了我的數據模型,但我無法訪問視圖中的類別。這裏是我的模型:MVC3 EF代碼第一個「外鍵」數據
namespace FFLettersMVC.Models
{
public class Letter
{
public int id {get; set;}
public DateTime dateCreated { get; set; }
public string letterTitle { get; set; }
public DateTime dateMailed { get; set; }
public string createdBy { get; set; }
public string Type { get; set; }
public int CategoryID { get; set; }
public Category Category { get; set; }
public virtual ICollection<Recipient> Recipient { get; set; }
}
public class Category
{
public int id { get; set; }
public string name { get; set; }
}
public class Recipient
{
public int id { get; set; }
public int letterID { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string ssnTin { get; set; }
public string email { get; set; }
public Letter Letter { get; set; }
}
}
控制器代碼詳情頁:
//
// GET: /Letter/Details/5
public ViewResult Details(int id)
{
Letter letter = db.Letters.Find(id);
return View(letter);
}
查看代碼試圖訪問評論屬性:
<div class="display-label">Category</div>
<div class="display-field">
@Html.DisplayFor(model => model.Category.name)
</div>
完美,工作。延遲3分鐘後,我會立即將其標記爲正確。 – Barnes 2012-04-18 12:56:21