I am trying to show the value of ViewBag on my .cshtml page, but the value is always empty. Below is my code:
public async Task<IActionResult> DateCalc( int? department)
{
ViewBag.DestroyCount = 4; // docDestroyed;
return RedirectToAction(nameof(Index));
}
This is what I have in my Index.cshtml :
<div >
<form asp-action="DateCalc" enctype="multipart/form-data">
<h5 >Documents destroyed by section</h5>
<div >
<div >
<label>Select Department(s)</label>
@Html.DropDownList("department", (IEnumerable
<SelectListItem>)ViewBag.department, new{@})
</div>
<div></div>
<br /><br />
<input type="submit" value="submit" />
<p >Documents Destroyed by Section: </p>@Html.Label((string)ViewBag.DestroyCount)
</div>
</form>
</div>
When I click on submit button, I want "Documents Destroyed by Section:" to display 4, but it does not display anything.
This is the line where I am trying to display DestroyCount
after clicking on submit button:
@Html.Label((string)ViewBag.DestroyCount)
Any help will be appreciated.
CodePudding user response:
The data for ViewBag.DestroyCount
was lost as you perform the redirection as:
public async Task<IActionResult> DateCalc(int? department)
{
...
return RedirectToAction(nameof(Index));
}
The data for ViewBag
and ViewData
is only lasted within the same request.