提问人:Tony Vitabile 提问时间:10/14/2023 更新时间:10/14/2023 访问量:23
如何创建具有 3 个视图模型的 ASP.NET MVC 3.2.7 页面
How to create an ASP.NET MVC 3.2.7 page with 3 view models
问:
我正在构建一个 ASP.NET MVC 3.2.7 网站。我有以下 Entity Framework 6.4.4 数据库模型:
[Table("Report")]
public class Report
{
[Key]
public class ReportId { get; set; }
// Other irrelevant properties
[NotMapped]
public IEnumerable<DataSection> DataSections
{
get
{
return ReportDataSections
.OrderBy(rds => rds.OrderSeq)
.Select(rds => rds.DataSection);
}
}
public virtual IColledction<ReportDataSection> ReportDataSections { get; } = new List<ReportDataSection>();
}
[Table("DataSection")]
public class DataSection
{
[Key]
public int DataSectionId { get; set; }
// Other irrelevant properties
[NotMapped]
public IEnumerable<Report> Reports
{
get
{
return ReportDataSections
.OrderBy(rds => rds.Report.ReportName)
.Select(rds => rds.Report);
}
}
public virtual ICollection<ReportDataSection> ReportDataSections { get; } => new List<ReportDataSection();
}
[Table("ReportDataSection")]
public class ReportDataSection
{
[Key]
[Column(Oder = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ReportId { get; set; }
[Key]
[Column(Oder = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int DataSectionId { get; set; }
public int OrderSeq { get; set; }
public virtual DataSection DataSection { get; set; }
public virtual Report Report { get; set; }
}
我希望网站的主页看起来像这样:
我有许多视图模型:
DataSourcesViewModel
:这有一个权利。ICollection<DataSourceViewModel>
ReportsViewModel
:这有一个权利。ICollection<ReportsViewModel>
DataSectionsViewModel
:这有一个权利。ICollection<DataSectionViewModel>
HomePageViewModel: this has three properties, a
DataSourcesViewModelReportsViewModelDataSectionsViewModle'。, and a
, a
这是课程:HomeController
public class HomePageController : Controller
{
private readonly HomePageViewModel _viewModel = new HomePageViewModel();
// GET: Home
public ActionResult Index()
{
return View();
}
// GET: DataSources
public ActionResult DataSources()
{
return View("DataSources", _viewModel.DataSources);
}
// GET: Reports
public ActionResult Reports()
{
return View("Reports", _viewModel.DataSources);
}
// GET: DataSections
public ActionResult DataSections()
{
return View("DataSections", _viewModel.DataSections);
}
}
此类不编译。我收到一个错误,因为类不是.然而,这段代码看起来就像我见过的其他例子,尤其是带有登录和注册表单的那个。ViewModel
IEnumerables
无论如何,我已经创建了部分视图,选项卡控件中的每个选项卡对应一个视图。该页面如下所示:Index
@model HomePageViewModel
@{
ViewBag.Title = "...";
ViewBag.DataSources = "...";
ViewBag.DatSections = "...";
ViewBag.Reports = "...";
}
<h2>@ViewBag.Title</h2>
<div class="tab-content">
<h4>@ViewBag.DataSources</h4>
@Html.RenderPartial("DataSources", Model.DataSources)
</div>
<div class="tab-content">
<h4>@ViewBag.Reports</h4>
@Html.RenderPartial("Reports", Model.Reports)
</div>
<div class="tab-content">
<h4>@ViewBag.DataSections</h4>
@Html.RenderPartial("DataSections", Model.DataSections)
</div>
这些都不是在建设。我不知道我做错了什么。我该如何让它工作。
答: 暂无答案
评论