我有一个视图来处理编辑操作,用于编辑您的体重和营养的每周更新。编辑一个单一的模型都很好。我正在使用 EditorFor 创建字段。
我的问题是我还想显示上周结果的只读版本作为指南,但我想使用 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);
}