Я пытаюсь показать данные с моей моделью,
У меня есть три таблицы: Регион, Город и Племя.
Племя
TribeId Pk
TribeName
CityId
Регион
RegionId pk
RegionName
Город
CityId
CityName
RegionId
Классы Entity Framework:
public partial class Region
{
public Region()
{
this.Cities = new HashSet<City>();
}
public int RegionId { get; set; }
public string RegionName { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
public class City
{
public City()
{
this.Asserahs = new HashSet<Asserah>();
this.Tribes = new HashSet<Tribe>();
}
public int CityId { get; set; }
public string CityName { get; set; }
public int RegionId { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<Asserah> Asserahs { get; set; }
public virtual ICollection<Tribe> Tribes { get; set; }
}
public class Tribe
{
public Tribe()
{
this.Asserahs = new HashSet<Asserah>();
}
public int TribeId { get; set; }
public string TribeName { get; set; }
public int? CityId { get; set; }
public virtual ICollection<Asserah> Asserahs { get; set; }
public virtual City City { get; set; }
}
Моя модель как,
public class TribeModel : DbContext
{
public DbSet<Tribe> Tribes { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Region> Regions { get; set; }
}
Я использую строительные леса для создания операции CRUD для Tribe.
Контроллер похож на:
public class TribesController : Controller
{
private StructureEntities db = new StructureEntities();
// GET: Tribes
public ActionResult Index()
{
var tribes = db.Tribes.Include(t => t.City);
return View(tribes.ToList());
}
// GET: Tribes/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tribe tribe = db.Tribes.Find(id);
if (tribe == null)
{
return HttpNotFound();
}
return View(tribe);
}
// GET: Tribes/Create
public ActionResult Create()
{
ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName");
return View();
}
// POST: Tribes/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "TribeId,TribeName,CityId")] Tribe tribe)
{
if (ModelState.IsValid)
{
db.Tribes.Add(tribe);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName", tribe.CityId);
return View(tribe);
}
// GET: Tribes/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tribe tribe = db.Tribes.Find(id);
if (tribe == null)
{
return HttpNotFound();
}
ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName", tribe.CityId);
return View(tribe);
}
// POST: Tribes/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "TribeId,TribeName,CityId")] Tribe tribe)
{
if (ModelState.IsValid)
{
db.Entry(tribe).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName", tribe.CityId);
return View(tribe);
}
// GET: Tribes/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tribe tribe = db.Tribes.Find(id);
if (tribe == null)
{
return HttpNotFound();
}
return View(tribe);
}
// POST: Tribes/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Tribe tribe = db.Tribes.Find(id);
db.Tribes.Remove(tribe);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
Таблица племени имеет идентификатор города, который является FK и идентификатор города PK в таблице городов он получает данные из этой таблицы. Идентификатор города имеет FK RegionId, то есть Pk в таблице регионов. Как мне указать регион в моей модели?