I am passing a ViewBag variable between my Controller and View and always has the same value of false. I verified during debugging that's it's not being overwritten.
The purpose of it is that under a certain condition, I want to display some additonal markup in my View if the ViewBag item is set to true. Right before the data gets sent back to the View, it is set to true, but during debugging in the View, it says it's false; thereby not displaying the additional markup that I want.
What am I doing incorrectly with the ViewBag item???
Here is the pertinent markup from the View:
if (ViewBag.Refresh == true)
{
<tr>
<td colspan="5">
<b>@Html.Label("lbl_Templates", "Templates:")</b>
</td>
</tr>
<tr>
@foreach (var item in Model.Templates)
{
<td>
@Html.CheckBoxFor(model => Convert.ToBoolean(item.IsChecked));
@Html.LabelFor(model => item.TemplateName)
</td>
}
</tr>
<tr>
<td colspan="5">
<b>@Html.Label("lbl_Guarantor", "Guarantor(s):")</b>
</td>
</tr>
<tr>
@foreach (var item in Model.Guarantors)
{
<td>
@Html.CheckBoxFor(model => Convert.ToBoolean(item.isChecked));
@Html.LabelFor(model => item.GuarantorFirstName + " " + item.GuarantorLastName)
</td>
}
</tr>
}
In the Controller, for the Index method, it's initially set to false:
public ActionResult Index()
{
ViewBag.Refresh = false;
ViewBag.Error = "";
return View();
}
Here is the Controller method. Note that it falls into the "else" logic. As soon as it goes back into the View (via the return Json statement):
[HttpPost]
public ActionResult Refresh(string loanID, string loanType, string selectedVal)
{
try
{
bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(loanID));
if (!dbHasRows)
{
ViewBag.Refresh = false;
ViewBag.Error = "Details not available for this LoanId";
return Json(new { success = false });
}
else
{
ViewBag.Refresh = true;
bool tmplst = true;
bool gurlst = true;
ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(loanID), loanType, selectedVal);
if (tg.Templates.Count() == 0)
{
tmplst = false;
ViewBag.Error = "Templates not available for this LoanType";
}
if (tg.Guarantors.Count() == 0)
{
gurlst = false;
ViewBag.Error = "Guarantors not available for this LoanId";
}
return Json(new { success = true, templist = tmplst, guarlist = gurlst });
}
}
catch (Exception ex)
{
throw ex;
}
}