Cannot perform runtime binding on a null reference ViewBag.Message

Viewed 34

Why I got this error with ViewBag.Message

"Cannot perform runtime binding on a null reference"

This is the controller code :

 public ActionResult Edit(List<RESULT> list)
        {
           
            if (ModelState.IsValid)
            {
                using (db_a85fa6_globalqualityEntities db = new db_a85fa6_globalqualityEntities())
                {
                    foreach (var i in list)
                    {
                        var c = db.RESULTS.Where(a => a.ID.Equals(i.ID)).FirstOrDefault();
                        if (c != null)
                        {
                            c.RESULT1 = i.RESULT1;
                        }
                    }
                    db.SaveChanges();
                    //return RedirectToAction("Details");

                }
                ViewBag.Message("Updated Successfully");
                return View(list);
                
            }
            else
            {
                ViewBag.Message("Failed ! Please try again");
                return View(list);
            }
          

        }

This is view code :

  <div class="form-group">
            <div class="col-md-offset-2 col-md-10" style="margin:100px;">
                <input type="submit" value="Save" class="btn btn-danger" />
               </div>
        </div>

    if (ViewBag.Message != null)
    {
        <p style="color:green;font-size:16px;">
            @ViewBag.Message
        </p>
    }

when click SAVE button its not show the message and show the error on this line of code :

 ViewBag.Message("Updated Successfully");

How to solve it ?

1 Answers

By looking at your code it looks like you are not using razor syntax with the if block. Try using razor syntax and check if it causing the same issue. Hope this helps.

Related