Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

Passing Error Message from Controller to ViewBag

Trying to figure out how to get a error message to the View Bag.  I have this in my View:

 @if (!string.IsNullOrEmpty(ViewBag.Message))
        <script type="text/javascript">
            title: "Error",
            text: "@ViewBag.Message",
            type: "error"
        }, function () {
            url = '@Url.Action("Index", "Requests")';
            window.location.href = url;

Open in new window

My Controller has both a Get and a Post and I'm not sure how to get the error message to my View in the ViewBag.Message

        public ActionResult Index()

            ViewBag.Message = String.Empty;
            if (!String.IsNullOrEmpty(HttpContext.Session["Error"]?.ToString()))
                ViewBag.Message = HttpContext.Session["Error"].ToString();

            return View();

        public ActionResult Upload(HttpPostedFileBase file, string Name)
            var result = new { Success = "", Message = "" };
                    string pdfTemplate = "NewRequest.pdf";
                    byte[] pdfbytes = null;
                    BinaryReader rdr = new BinaryReader(file.InputStream);
                    pdfbytes = rdr.ReadBytes((int)file.ContentLength);
                    PdfReader reader = new PdfReader(pdfbytes);

                    string t = Name.Trim().Length == 0 ? null : Name;
                    var fields = reader.AcroFields.Fields;
                    var RequestID = reader.AcroFields.GetField("requestId");
                    var OpenDate = reader.AcroFields.GetField("openDate");
                    var Org = reader.AcroFields.GetField("organization");
                    var NeedDate = reader.AcroFields.GetField("needDate");
                    var FirstName = reader.AcroFields.GetField("firstName");
                    var LastName = reader.AcroFields.GetField("lastName");
                    tbl_Request request = new tbl_Request()
                        addedDate = DateTime.Now,
                        title = t,
                        requestId = RequestID,
                        openDate = Convert.ToDateTime(OpenDate),
                        organization = Org,
                        needDate = Convert.ToDateTime(NeedDate),
                        firstName = FirstName,
                        lastName = LastName


                catch (Exception ex)
                    Utility util = new Utility();
                    string error = util.innerExcep(ex);
                result = new { Success = "False", Message = error };
                return RedirectToAction("Index", "Upload", new { Result = result });

            return RedirectToAction("Index", "Requests", new { });

Open in new window

Avatar of kaufmed
Flag of United States of America image

ViewBag's data type is dynamic. That means you won't get any Intellisense on it. Other than that, just assign to it at whatever place in your controller is appropriate:

ViewBag.Message = "Uh-oh!!";

Open in new window

Avatar of Crystal Rouse


Can you please give an example of how to use it in the View?
That's line 1 of your first code snippet.
I tried setting ViewBag.Message = "Test" in the controller.  I get nothing.  
I'm not doing something right here.
Avatar of kaufmed
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much for the sample code!