How to display additional readonly view data in Edit view MVC
-
31-10-2019 - |
Question
I have a View that handles the Edit action for editing a weekly update on your weight and nutrition. Editing a singular model is all good. I am using EditorFor to create the fields.
My problem is that I want to also display a read only version of last week's results as a guide but I would like to use DisplayFor so that it formats bools to be disabled checkboxes and formats the dates based on my formatting in the model. I added the model to the Viewbag and tried to access it by using @Html.DisplayFor(x => x.BodyWeight, (myproject.Models.WeeklyReport)ViewBag.LastReport) however it just brings up the data in the model that I sent to the view and not the Viewbag data. What is the best method to display this kind of data while keeping the constraints/formatting of the model intact?
Thanks.
View
@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>
Controller
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);
}
No correct solution