1

我正在为我的租户控制器使用创建方法。每个新租户都有一个与之相连的相应建筑物。我想要它,以便在我的创建视图中显示建筑物名称,然后用相应的 id 填充表。我无法弄清楚如何让它发挥作用。

public class Renter
    {
        [Key]
        public int RenterId { get; set; }
        public int BuildingID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        [DataType(DataType.PhoneNumber)]
        public String Phone { get; set; }
        [DataType(DataType.EmailAddress)]
        public String Email { get; set; } 
        public Boolean IsApproved { get; set; }

        //Foreign Key
       // [Required]
        public virtual Building Building { get; set; }
    }

public class Building
    {
        [Key]
        public int BuildingId { get; set; }
        public string BuildingName { get; set; }
        public virtual ICollection<Renter> Residents { get; set; }
        public int ManagerId { get; set; }
        public virtual Manager Manager { get; set; }
    }
 public class Manager
    {
        [Key]
        public int ManagerId { get; set; }
        public virtual ICollection<Renter> Tenants { get; set; }
        public virtual ICollection<Building> Buildings { get; set; }

    }

创建视图

@model RPMS.Models.Renter

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Renter</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.BuildingID, "Building")
        </div>
        <div class="editor-field">
            @Html.DropDownList("BuildingID", String.Empty)
            @Html.ValidationMessageFor(model => model.BuildingID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IsApproved)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IsApproved)
            @Html.ValidationMessageFor(model => model.IsApproved)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

目前,当我运行它并去选择建筑物时,即使我已经初始化了数据,下拉框中也没有显示任何内容

protected override void Seed(RPMSContext context)
        {
            var renters = new List<Renter>
            {
            new Renter {RenterId = 1, BuildingID = 1, FirstName="Bradley", LastName="Woods",Phone="614-218-0294", Email="bradleydeanwoods@gmail.com",IsApproved = true},

            };
            renters.ForEach(s => context.Renters.Add(s));
            context.SaveChanges();

            var buildings = new List<Building>
            {
                new Building {BuildingId = 1, BuildingName="Annex at River South",ManagerId = 1},
            };
            buildings.ForEach(s => context.Buildings.Add(s));
            context.SaveChanges();

            var managers = new List<Manager>
            {
                new Manager {ManagerId = 1,},
            };
            managers.ForEach(s => context.Managers.Add(s));
            context.SaveChanges();
        }

最后是 DbContext

public class RPMSContext : DbContext
    {
        public DbSet<Renter> Renters { get; set; }
        public DbSet<Building> Buildings { get; set; }
        public DbSet<Manager> Managers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

提前感谢您的帮助,帮助我克服了这个症结。

更新种子数据显示我何时取出外键关系,这就是我现在所做的,只是决定继续前进。

4

0 回答 0