[英]MVC + EF Code First Many to Many relationship: Create view with checkbox
我是MVC + EF Code First的新手,對於很多關系都有一些疑問。
我創建了兩個具有多對多關系的表(userprofile和courses)。 聽起來很簡單,但對我來說非常復雜:
我希望看到“userprofile”的“創建”視圖,其中包含每個“課程”現有選項的復選框,並且能夠將其保存到相應的數據庫。
我一直在閱讀和尋找示例,但我沒有解決這個問題(或者找到關於這個特殊需求的任何相關信息(我發現類似的事情不適用)
我已經看過這篇文章,但它不是關於創建而不是使用EF(http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-所述實體框架功能於一個-ASP凈MVC施用)
public class UserProfile
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Courses> usercourses { get; set; }
}
public class Courses
{
public int CourseID { get; set; }
public string CourseDescripcion { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
public class UserProfileDBContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Courses> usrCourses{ get; set; }
}
創建控制器:
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(UserProfile userprofile)
{
if (ModelState.IsValid)
{
db.UserProfiles.Add(userprofile);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userprofile);
}
關於視圖,我想只有'@foreach'循環來生成dropbox。
你在我回答的另一篇文章中問過幾乎同樣的問題。 我已經逐步布局如何做到這一點你不會出錯:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.