私はあなたの体重と栄養に関する毎週の更新を編集するための編集アクションを処理するビューを持っています。単一のモデルを編集することはすべて良いことです。EditorForを使用してフィールドを作成しています。
私の問題は、先週の結果の読み取り専用バージョンもガイドとして表示したいのですが、DisplayForを使用して、boolsを無効にするチェックボックスをフォーマットし、モデルのフォーマットに基づいて日付をフォーマットしたいと思います。モデルをViewbagに追加し、@ Html.DisplayFor(x => x.BodyWeight、(myproject.Models.WeeklyReport)ViewBag.LastReport)を使用してアクセスしようとしましたが、送信したモデルのデータが表示されるだけです。ビューバッグデータではなく、ビューに。モデルの制約/フォーマットをそのままに保ちながら、この種のデータを表示するための最良の方法は何ですか?
ありがとう。
意見
@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);
}