0

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;
            }
        }
sagesky36
  • 4,542
  • 19
  • 82
  • 130
  • 1
    You're returning Json. That would imply you are not returning a view, but rather an Ajax request. Since Ajax occurs from the client side, with no view at all... I don't see why you would expect a view to be able to reference something that was done in a different action. Where are you rendering your view anyways? – Erik Funkenbusch Oct 02 '12 at 20:38
  • You're correct. I didn't know that.... However, I'm having a major issue with this request and was hoping that someone could set me straight on this. Could somebody please take a look at this link and inform me what I might be doing wrong? http://forums.asp.net/t/1848093.aspx/1?Transferring+ViewBag+variable+between+Controller+and+View – sagesky36 Oct 03 '12 at 00:59
  • I would put the stuff in the If(ViewBag.Refresh){} part in a
    tag with display: none; set. Then in your Json, return an additional variable (refresh=true|false} and based on the JSON return, show or hide the
    . If you actually need stuff back from the AJAX request to render the view, maybe look into returning a partial view -> http://stackoverflow.com/questions/4730777/mvc-return-partial-view-as-json
    – Tommy Oct 03 '12 at 02:49
  • I did do exactly as you said even prior to your comment. However, I wasn't getting back into my javascript on the return Json statement and I don't know why... – sagesky36 Oct 03 '12 at 11:34

0 Answers0