レンターコントローラーの作成メソッドを使用しています。それぞれの新しい賃借人には、それに付随する対応する建物があります。作成ビューで建物の名前を表示し、対応する 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>();
}
}
この困難な点を乗り越えるのを手伝ってくれてありがとう。
更新シードデータは、外部キー関係を取り出したときに表示されるため、これまでに行ったことであり、先に進むことにしました。