MVC 5码首先脚手架用简单的关系 [英] MVC 5 Code First scaffolding with simple relationship

查看:254
本文介绍了MVC 5码首先脚手架用简单的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些实验编程陷入了ASP MVC。

I'm doing some experimental programming to get caught up with ASP MVC.

我创建了包含客房的建筑项目。一个非常简单的一对多的关系。我试图让脚手架的工作,并从旧MVC的例子,它看起来像这应该只是工作。然而,在该酒店的客房现场BuildingId不映射到建筑模型 - 视图没有选择列表

I created a project for buildings containing rooms. A very simple one to many relationship. I am trying to get scaffolding to work, and from older MVC examples it looks like this should just work. However, the BuildingId field in Rooms isn't mapping to the Building model - no select list in the view.

我的型号是:

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; }        
    }
 }



我产生的控制器使用实体框架的意见,它创造的形式,但不与房间编辑视图预期大厦选择列表中。它显示一个整数输入字段来代替。

I generated the controller with views using Entity Framework, it created the forms but not with the expected Building select list in the Room edit view. It displays an integer input field instead.

我缺少什么

推荐答案

您应该改变这样的:

public class Room
{
    public int Id { get; set; }

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



to

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;}
}

这路脚手架会为建设一个选择列表。

This way the scaffolding will generate a select list for the building.

这篇关于MVC 5码首先脚手架用简单的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆