VIEWBAG IN MVC 4

ezra heywood

Total Post:145

Points:1019
Posted by  ezra heywood
 1228  View(s)
Ratings:
Rate this:
I am using a ViewBag as my checkpoint to which partial view should I render on a certain div and here's my code.
In Controller:

[HttpPost]

        public ActionResult NewAppointment(appointment.AppointmentInformation model)

        {

            if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")

            {

                info.CustomerType = model.CustomerType;

                ViewBag.NextForm = "Information";

            }

            else if (ViewBag.NextForm == "Information")

            {

                info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID;

                info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName;

                info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum;

                ViewBag.NextForm = "Services";

            }

            else if (ViewBag.NextForm == "Services")

            {

                //do nothing;

            }

            return View(info);

        }

In View:

<form method="post">

    <div id="PartialContainer">

 

        @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")

        {

            @Html.Partial("CustomerType")

        }

        @if (ViewBag.NextForm == "Information")

        {

            @Html.Partial("GuestInformation")

        }

        @if (ViewBag.NextForm == "Services")

        {

            @Html.Partial("Service")

        }

 

    </div>

    <div id="ButtonArea">

        <button id="btnCancel">Cancel</button>

        <button id="btnBack">Back</button>

        <button id="btnNext">Next</button>

    </div>

</form>


On third click of btnNext, the @Html.Part @Html.Partial("Service") is not working. But the first two @Html.Partial is working fine.

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: viewbag in mvc 4

    Why don't you embed the step in your form (in your partials)?

    That way, whenever the form is submitted, you get which step it was, and show the proper next step from your controller code, not the view.

    You could either add it as a property of your ViewModel, or simply just POST it and get it from Request object in your Controller.

    Pseudo-code:

    [HttpPost]

            public ActionResult NewAppointment(appointment.AppointmentInformation model)

            {

                //get the current step or start with empty string

                //although if this is the POST method, you should have the

                //first value, set in your GET method to show the form!

                var step = Request.Params["NextForm"] ?? "";

     

                if (step == "")

                {

                    info.CustomerType = model.CustomerType;

                    ViewBag.NextForm = "Information";

                }

                else if (step == "Information")

                {

                    info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID;

                    info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName;

                    info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum;

                    ViewBag.NextForm = "Services";

                }

                else if (step == "Services")

                {

                    //do nothing;

                }

                return View(info);

            }

    And then use that value in your View to have it sent anytime with your form.

    <form method="post">

        <div id="PartialContainer">

            <input type="hidden" name="NextForm" value="@(ViewBag.NextForm ?? "")" />

     

            @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")

            {

                @Html.Partial("CustomerType")

            }

            @if (ViewBag.NextForm == "Information")

            {

                @Html.Partial("GuestInformation")

            }

            @if (ViewBag.NextForm == "Services")

            {

                @Html.Partial("Service")

            }

     

        </div>

        <div id="ButtonArea">

            <button id="btnCancel">Cancel</button>

            <button id="btnBack">Back</button>

            <button id="btnNext">Next</button>

        </div>

    </form>

    You will be getting the NextStep with your form everytime, and then you just use ViewBag to pass data from controller to view, which is the intended use.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!