在Controller和View之间传输ViewBag变量

时间:2012-10-02 20:27:09

标签: asp.net-mvc viewbag

我在Controller和View之间传递一个ViewBag变量,并且始终具有相同的false值。我在调试期间验证了它没有被覆盖。

它的目的是在某个条件下,如果ViewBag项设置为true,我想在我的View中显示一些额外的标记。在数据发送回View之前,它被设置为true,但在View中调试期间,它表示它是错误的;从而不显示我想要的附加标记。

我对ViewBag项目做错了什么???

以下是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>
                            }

在Controller中,对于Index方法,它最初设置为false:

public ActionResult Index()
        {
            ViewBag.Refresh = false;
            ViewBag.Error = "";
            return View();
        }

这是Controller方法。请注意,它属于“其他”逻辑。一旦它返回到View(通过返回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;
            }
        }

0 个答案:

没有答案