我在控制器和视图之间传递了一个 ViewBag 变量,并且始终具有相同的 false 值。我在调试期间验证它没有被覆盖。
它的目的是在某种情况下,如果 ViewBag 项设置为 true,我想在我的视图中显示一些附加标记。在数据被发送回视图之前,它被设置为真,但在视图中调试期间,它说它是假的;从而不显示我想要的附加标记。
我在 ViewBag 项目上做错了什么???
这是视图中的相关标记:
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>
}
在 Controller 中,对于 Index 方法,它最初设置为 false:
public ActionResult Index()
{
ViewBag.Refresh = false;
ViewBag.Error = "";
return View();
}
这是控制器方法。请注意,它属于“其他”逻辑。一旦它回到视图中(通过 return Json 语句):
[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;
}
}