0

I need to get a record count out of a successful Ajax post.

Since the datatype is "html", I cannot check for a specific record count via json.

So, what I'm doing is trying to apply the record count to a ViewBag variable (from a Controller method) and assign it to a hidden html field that I can check after the post. If 0 records are returned, I can display a proper message to the user.

My process is as follows: I'm using MVC 5.2

  1. In the GET for the Controller method, I set the ViewBag variable count to 1 (for a test. Ideally, it should be 0).
    1. The View assigns the value of the ViewBag variable to the JS variable.
    2. In the POST, I get the record count from the records retrieved and assign it to the ViewBag property.
    3. The View again should assign the value of the ViewBag variable to the JS variable.
    4. When the POST finishes, the JS variable checks if the value is 0 and then would display the proper message.

However, the issue I'm having is that the initial value I set during the GET of 1, never changes.

I've verified during debugging, that when the POST occurs in the controller, the ViewBag property is set to 0 (no records returned). I purposely set the initial value to 1 (in the GET) just to see if the value has changed.

Can someone please inform me why the JS variable is not being updated to the value of 0 (step 4 above) after the POST?

Here is my code:

Controller

// GET: Customers/CustomerProjects/Details
public async Task<ActionResult> Details()
        {
            ViewBag.projectCount = "1";
            ViewBag.CustomerEmail = await db.GetCustomerDDLAsync();

            return View();
        }

[HttpPost]
        // POST: Customers/CustomerProjects/Details/5
        public async Task<ActionResult> Details(short? id)
        {
            if (id == null)
            {
                return View("IDIsNull");
            }
            ViewBag.CustomerEmail = await db.GetCustomerDDLAsync();
            Customer customer = await db.GetProjectsByCustomerIDAsync(id);
            if (customer == null)
            {
                return View("ObjectModelNull");
            }

            ViewBag.projectCount = customer.Projects.Count.ToString();
            return View(customer);
        }

VIEW

@Html.Hidden("projCnt", (string)(ViewBag.projectCount))

JS

if ($('#projCnt').val() == "0")
sagesky36
  • 4,542
  • 19
  • 82
  • 130

2 Answers2

0

Viewbag only exist for 1 view,

When you are transfered to the POST, the Viewbag is empty.

You can use TempData instead.

Check out tihs link:

Tempdata vs Viewbag...

edit: Also, it is not that clear, maybe you're Return View occurs before you update the ViewBag? (i.e. goes into one of the if's)

Community
  • 1
  • 1
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61
  • I tried Tempdata. That doesn't work either...The GET & POST are using both the same View. My return View definitely occurs after I update the ViewBag. I verified it during debugging. – sagesky36 Feb 12 '15 at 17:37
  • Strange... can you post your full code then? You can also try using the Session object, to see if this is the problem. (i.e. Session["value"] = "1" ) then use instead of the ViewBag.projectCount;) – Ziv Weissman Feb 14 '15 at 00:03
  • Ziv, I got around this issue by setting the datatype of the ajax call from 'html' to 'json' and was able to detect the count in the 'success' function to display the proper message. – sagesky36 Feb 14 '15 at 19:57
0

try this:

 TempData["projectCount "] = 123;
 TempData.Keep();
free4ride
  • 1,613
  • 1
  • 18
  • 21