【问题标题】:MVC 5 Code First scaffolding with simple relationship具有简单关系的 MVC 5 Code First 脚手架
【发布时间】:2014-03-29 13:25:28
【问题描述】:

我正在做一些实验性编程来了解 ASP MVC。

我为包含房间的建筑物创建了一个项目。一个非常简单的一对多关系。我正在尝试让脚手架工作,并且从较旧的 MVC 示例看来,这应该可以正常工作。但是,Rooms 中的 BuildingId 字段没有映射到 Building 模型 - 视图中没有选择列表。

我的模型是:

namespace BuildingManagement.Models
{
    public class Building 
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }
        public string Address { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string PostalCode { get; set; }

        [Display(Name = "Phone")]
        [DataType(DataType.PhoneNumber)]
        [Required]
        public string PhoneMain { get; set; }

        [Display(Name = "Contact")]
        [Required]
        public string ContactName { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Room> Rooms { get; set; }      

    }
}

namespace BuildingManagement.Models
{
    public class Room
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }
        public string Type { get; set; }
        public int BuildingId { get; set; }        
    }
 }

我使用 Entity Framework 生成了带有视图的控制器,它创建了表单,但没有使用 Room 编辑视图中的预期建筑选择列表。它改为显示一个整数输入字段。

我错过了什么?

【问题讨论】:

    标签: c# entity-framework visual-studio-2013 asp.net-mvc-5


    【解决方案1】:

    你应该改变这个:

    public class Room
    {
        public int Id { get; set; }
    
        [Required]
        public string Name { get; set; }
        public string Type { get; set; }
        public int BuildingId { get; set; }        
    }
    

    public class Room
    {
        public int Id { get; set; }
    
        [Required]
        public string Name { get; set; }
        public string Type { get; set; }
    
        [ForeignKey("ContainingBuilding")]
        public int BuildingId { get; set; }      
    
        public virtual Building ContainingBuilding{ get; set;}
    }
    

    这样脚手架将为建筑物生成一个选择列表。

    【讨论】:

    • 我认为这让我更接近。现在我得到一个导航属性错误The ForeignKeyAttribute on property 'BuildingId' on type 'FacilityManagement.Models.Room' is not valid. The navigation property 'Building' was not found on the dependent type 'BuildingManagement.Models.Room'. The Name value should be a valid navigation property name.
    • 有时不可能有同名的类和属性,正如我在这里展示的那样。我会编辑答案。
    【解决方案2】:
    public List<SelectListItem> CountryListItems {get; set;}
    public int CountryId {get; set;}
    

    上面的示例模型

       Model.CountryListItems= new List<SelectListItem>();
       CountryListItems.Add(new SelectListItem
            {
              Text = "Albania",
              Value = "1"
            });
       CountryListItems.Add(new SelectListItem
            {
                Text = "Bangladesh",
                Value = "2",
                Selected = true
            });
       CountryListItems.Add(new SelectListItem
            {
                Text = "Canada",
                Value = "3"
            });
    

    上面的示例代码可以在控制器或其他生成器模型类中使用。

    @Html.DropDownListFor(model => model.CountryId, model.CountryListItems, "-- Select Status --")
    

    上面的示例视图块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      相关资源
      最近更新 更多