0
我有一个处理编辑操作的视图,用于编辑体重和营养的每周更新。编辑单一模型是很好的。我正在使用EditorFor创建字段。如何在编辑视图中显示其他只读视图数据MVC
我的问题是,我还想显示上周结果的只读版本作为指南,但我想使用DisplayFor,以便它格式化布尔被禁用复选框并格式化日期基于我的格式模型。我将该模型添加到Viewbag中,并试图通过使用@ Html.DisplayFor(x => x.BodyWeight,(myproject.Models.WeeklyReport)ViewBag.LastReport)来访问它,但它只是调出我发送的模型中的数据到视图而不是Viewbag数据。在保持模型的约束/格式完好的同时显示这种数据的最佳方法是什么?
谢谢。
查看
@model myproject.Models.WeeklyReport
<h2>Weekly Report - Week 1</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table class="weeklyreport">
<tr>
<th>Week</th>
<td class="result-bold">Goals</td>
<td>Current Week</td>
</tr>
<tr>
<th>Body Weight</th>
<td class="result-bold">@Html.DisplayFor(x => x.BodyWeight, (myproject.Models.WeeklyReport)ViewBag.Goals)</td>
<td>@Html.EditorFor(model => model.BodyWeight)
@Html.ValidationMessageFor(model => model.BodyWeight)</td>
</tr>
<tr>
<th>Diary Reviewed</th>
<td class="result-bold">@Html.DisplayFor(x => x.DiaryReviewed, (myproject.Models.WeeklyReport)ViewBag.Goals)</td>
<td>@Html.EditorFor(model => model.DiaryReviewed)
@Html.ValidationMessageFor(model => model.DiaryReviewed)</td>
</tr>
</table>
控制器
public ActionResult Edit(int id)
{
WeeklyReport goal = new WeeklyReport()
{
BodyWeight = 60,
DiaryReviewed = true
};
WeeklyReport rpt = new WeeklyReport()
{
BodyWeight = 68,
DiaryReviewed = false
};
ViewBag.LastReport = goal;
return View(rpt);
}