---
title: "viewbag in mvc 4"  
description: "viewbag in mvc 4"  
author: "Anonymous User"  
published: 2014-10-09  
updated: 2014-10-09  
canonical: https://www.mindstick.com/forum/2347/viewbag-in-mvc-4  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc"]  
reading_time: 3 minutes  

---

# viewbag in mvc 4

I am using a [ViewBag](https://www.mindstick.com/forum/155739/define-viewbag-in-mvc) as my [checkpoint](https://www.mindstick.com/blog/164/checkpoints-in-sqlserver) to which [partial view](https://www.mindstick.com/articles/1132/auto-refresh-partial-view-in-asp-dot-net-mvc) should I [render](https://www.mindstick.com/interview/2205/why-we-need-a-separate-mobile-project-template-while-we-can-render-our-web-application-in-mobile-what-s-new-in-mvc-4-mobile-template) on a certain div and here's my code.**In [Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc):**\

```
[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](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) of btnNext, the @Html.Part @[Html.Partial](https://www.mindstick.com/forum/2318/what-is-the-difference-between-html-partial-and-html-renderpartial-and-html-action-and-html-renderaction)("[Service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities)") is not working. But the first two @Html.Partial is [working fine](https://answers.mindstick.com/qa/95550/why-is-the-safari-browser-not-working-fine).\

## Replies

### Reply by Anonymous User

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.\


---

Original Source: https://www.mindstick.com/forum/2347/viewbag-in-mvc-4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
