ViewBag Property not displaying in View

user1870218

Im trying to show the message from an exception on one of my views using a ViewBag property. However, no matter what I put in the ViewBag property it does not show in my view.

I have tried sending the exception and a simply string, this is the method from the controller:

        public ActionResult Create([Bind(Include="IDHardware,PartNo,SerialNo,CatagoryType,IsOnLoan")] Hardware hardware)
    {
        if (ModelState.IsValid)
        {
            try
            {
                db.Hardwares.Add(hardware);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                ViewBag.ErrMsg = exceptionMessage;


                return RedirectToAction("BarcodeNotUnique");
            }

        }

        ViewBag.CatagoryType = new SelectList(db.Catagories, "CatagoryName", "CatagoryName", hardware.CatagoryType);
        return View(hardware);

And this is the html from the view BarcodeNotUnique:

    @model TechDemoStockWebsite.Models.Hardware

@{
    ViewBag.Title = "BarcodeNotUnique";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="jumbotron">
    <h1>@ViewBag.ErrMsg</h1>
    <h1>@ViewBag.example</h1>
    <p class="lead"> @Html.ActionLink("Please try again", "Create")</p>
    <p class="lead"> or return to list of @Html.ActionLink("available hardware", "Index")</p>

</div>

Any ideas?

BR

Chris

David Spence

Anything in ViewData gets lost if you do a redirect. In your code, you are setting a property on ViewData and then performing return RedirectToAction("BarcodeNotUnique");. If you really need to do a redirect, you can use TempData instead.

See this question for more details about ViewData and TempData: ViewBag, ViewData and TempData

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I set a ViewBag property in an ASP.NET view?

From Dev

MVC 4: Displaying a List<> property on a view

From Dev

Displaying the Value of a ViewBag in the EditorFor() method of a View using ASP.NET MVC

From Dev

Assigning ViewBag property in javascript

From Dev

Passing viewbag result to view

From Dev

When overloading a Controller that returns a View, how should I load different content based on a ViewBag property?

From Dev

When overloading a Controller that returns a View, how should I load different content based on a ViewBag property?

From Dev

C# Displaying a ViewBag based on ViewModel controller

From Dev

Access Viewbag property on all views

From Dev

pass model to view using viewbag

From Dev

It is possible to refresh ViewBag value in the view?

From Dev

Pass an array to the view using viewbag

From Dev

Displaying data on property grid

From Dev

Displaying data on property grid

From Dev

Displaying a property of a class

From Dev

How to check if ViewBag property is null or not exists

From Dev

Getting viewbag dynamic property's value as null

From Dev

get value from viewbag property in javascript

From Dev

How to check ViewBag's property in a cshtml page?

From Dev

Displaying attribute of ViewBag depending on the selected item from DropDownList and posting the ID

From Dev

MVC variables not displaying in view

From Dev

Displaying string in view?

From Dev

Backbone view not displaying

From Dev

addChildViewController view not displaying

From Dev

Displaying scrape to view

From Dev

Displaying Associated Data in View

From Dev

displaying days of week in view

From Dev

Surface view not displaying anything

From Dev

Displaying a separator view in CollectionView

Related Related

HotTag

Archive