Asp.net
MVC 5 單個視圖中的多個模型
有人可以提供一個如何在一個視圖中組合兩個模型的範例嗎?
目前我有一個名為 RecordCard 的頁面,其中包含:
@model IEnumerable<WebApplication1.Models.Weight>這是由 AccountController 中的以下程式碼提供的:
public ActionResult RecordCard() { var UserId = User.Identity.GetUserId(); var weightModel = from m in db.Weights where m.UserId == UserId select m; return View(weightModel); }RecordCard 頁面還包含一個綁定到以下類的表單:
public class AddWeightModel { [Required] [DataType(DataType.Text)] [Display(Name = "Stone")] public Nullable<short> Stone { get; set; } [Required] [DataType(DataType.Text)] [Display(Name = "Pound")] public Nullable<short> Pound { get; set; } }但是,這是兩個具有不同目的的單獨模型,所以我如何組合到一個模型,其中包含一個 IEnumerable 列表和一組表單元素,這些元素最終將正確發佈到 AccountController 以使用以下程式碼將記錄添加到數據庫:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult RecordCard(Weight Model) { if (ModelState.IsValid) { using (WebApplication1Entities db = new WebApplication1Entities()) { Weight weight = new Weight(); weight.UserId = User.Identity.GetUserId(); weight.Stone = Model.Stone; weight.Pound = Model.Pound; weight.Date = System.DateTime.Now; db.Weights.Add(Model); db.SaveChanges(); } } return View(Model); }我在下麵包含了重量類:
public partial class Weight { public int Id { get; set; } public string UserId { get; set; } public Nullable<short> Stone { get; set; } public Nullable<short> Pound { get; set; } public Nullable<System.DateTime> Date { get; set; } }這裡還有將權重表聲明為權重的 WebApplication1Entities 類:
public partial class WebApplication1Entities : DbContext { public WebApplication1Entities() : base("name=WebApplication1Entities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Weight> Weights { get; set; } }請解釋需要修改的內容以及無論我嘗試閱讀、遵循和實施什麼,我似乎都遺漏了一些東西。
任何幫助將非常感激 :-)
我以前解決過這個問題,可以找到一個優雅的解決方案。
首先,您需要設置要發送的主要類,以及用於儲存它們以最終發送到
view.正如您可能發現的那樣,這是因為
view不能將多個模型發送給它。public class WebsiteTheme { public string Color { get;set; } public string Title { get;set; } public WebsiteTheme() { Color = "blue"; Title = "test website"; } } public class User { public string Name { get;set; } public string Gender { get;set; } public User() { Name = "Anonymous"; Gender = "Unspecified"; } } public class ToPage { public WebsiteTheme WebsiteTheme{ get; set; } public User User { get; set; } public ToPage() { websiteTheme = new WebsiteTheme(); user = new User(); } }這將允許您將任意數量的課程發送到您的頁面。
然後,在您的控制器中,您需要填充這些類。確保首先將它們全部初始化,然後將填充的類設置為您的持有者類。
WebsiteTheme websiteTheme = new WebsiteTheme(); websiteTheme.Color = "orange"; User user = new User(); user.Name = "Darren"; ToPage toPage = new ToPage(); toPage.User = user; toPage.WebsiteTheme = websiteTheme; return View(toPage);在您看來,您可以以任何您想要的方式呼叫它們。但請確保
HolderModel.SpecifiedModel在每種情況下都使用。@model WebApplication1.Models.ToPage @Html.DisplayFor(model => model.User.Name)
我會說這是在這裡使用的好例子
ViewModel。我會建議像 -
ViewModel用兩個類的組合創建public class AddWeightModel { [Required] [DataType(DataType.Text)] [Display(Name = "Stone")] public Nullable<short> Stone { get; set; } [Required] [DataType(DataType.Text)] [Display(Name = "Pound")] public Nullable<short> Pound { get; set; } } .... public partial class Weight { public int Id { get; set; } public string UserId { get; set; } public Nullable<short> Stone { get; set; } public Nullable<short> Pound { get; set; } public Nullable<System.DateTime> Date { get; set; } } ..... public class WeightViewModel { public IList<AddWeightModel> AddWeightModel { get; set; } public Weight Weight { get; set; } }然後更改您的視圖以接受視圖模型 -
@model WeightViewModel最後修改您的控制器以應對變化 -
public ActionResult RecordCard() { var UserId = User.Identity.GetUserId(); var weightModel = from m in db.Weights where m.UserId == UserId select m; var viewModel = new WeightViewModel { Weight = weightModel, AddWeightModel = new List<AddWeightModel>(){} }; return View(viewModel); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult RecordCard(WeightViewModel viewModel) { Weight Model = viewModel.Weight; if (ModelState.IsValid) { using (WebApplication1Entities db = new WebApplication1Entities()) { Weight weight = new Weight(); weight.UserId = User.Identity.GetUserId(); weight.Stone = Model.Stone; weight.Pound = Model.Pound; weight.Date = System.DateTime.Now; db.Weights.Add(Model); db.SaveChanges(); } } return RedirectToAction("RecordCard"); }