如何在MVC视图上访问外键值?

时间:2016-12-13 15:31:22

标签: sql-server model-view-controller foreign-keys foreign-key-relationship

我在视图中访问外键值时遇到了问题而没有使用部分内容。

我将tblProperty作为Primary_Key并将tblCustomer作为foreign_key。我想在我的视图中访问外键的值,但无法弄清楚原因。

  

模型

  public partial class tblProperty
{
    public tblProperty()
    {
        this.Images = new HashSet<Image>();
        this.tblCustomers = new HashSet<tblCustomer>();
    }

    public int propertyID { get; set; }
    public string address { get; set; }
    public string description { get; set; }
    public virtual ICollection<Image> Images { get; set; }
    public virtual ICollection<tblCustomer> tblCustomers { get; set; }
 }

 public partial class tblCustomer
{
    public int customerID { get; set; }
    public string name { get; set; }
    public decimal contactNumber { get; set; }
    public string notes { get; set; }
    public Nullable<int> propertyID { get; set; }   
    public virtual tblProperty tblProperty { get; set; }
}
  

控制器

 public class propertyController : Controller
{

    propertyDBEntities2 dc = new propertyDBEntities2();


    public ActionResult List()
    {

       var properties = dc.tblProperties.Include(p => p.tblCustomers);
        return View(properties.ToList());
    }

    public ActionResult Details(int id = 0)
    {
        var properties = dc.tblProperties.Include(p => p.tblCustomers);
        tblProperty property = dc.tblProperties.Find(id);
        tblCustomer customer = dc.tblCustomers.Find(id);
        if (properties == null)
        {
            return HttpNotFound();
        }
        return View(dc.tblProperties.Find(id));
    }
    public ActionResult Create()
    {
        return View();
    }
    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Create(tblProperty e)
    {
        if (ModelState.IsValid)
        {
            using (dc)
            {
                dc.tblProperties.Add(e);
                dc.SaveChanges();
            }
        }
        return RedirectToAction("List");
    }
  

视图

(就像model.name试图从tblCustomer访问名称)

 @model myProject.tblProperty
 @Html.DisplayFor(model => model.name) 

1 个答案:

答案 0 :(得分:0)

tblProperty没有名字。

我想你需要

  @Html.DisplayFor(model => model.tblCustomer.name) 

但只是调试它或使用intellisense

修改

在我的项目中,我创建了一个dtoClass 数据传输对象

因此,对于我的avl课程,我有一个dtoAvl

avl课程:

public partial class avl
    {
        public avl()
        {
            this.cars = new HashSet<cars>();
        }

        public long avl_id { get; set; }
        public Nullable<long> car_id { get; set; }
        public Nullable<decimal> speed { get; set; }

        // this class contain info regarding the road
        public virtual manila_rto manila_rto { get; set; } 
        public virtual ICollection<cars> cars { get; set; }
    }

我创建了一个dtoAvl

public class dtoAvl
{
    public long Avl_ID { get; set; }
    public long? Car_ID { get; set; }
    public string RoadName { get; set; } // came from manila_rto 
    public int Speed { get; set; }
}

我的控制器

List<dtoAvl> result = db.avls.Select(
                              r => new dtoAvl
                              {
                                  Avl_ID = r.Avl_ID,
                                  Car_ID  = r.Car_ID,
                                  Speed  = r.Speed,

                                  // here is a propery but can be a list
                                  RoadName = r.manila_rto.name 
                              }).ToList();
return PartialView(result);

查看:

@model IEnumerable<dtoAvl>