Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can ViewBag data be saved after a form post?

So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so @Html.LabelFor(m => m.Something, (string)ViewBag.Something). This works out well but when I submit the form there could be errors and if there are errors, I need this label to remain the same, I don't want dynamic/random data anymore so I wouldn't call the controller method that generated this ViewBag. Is there a way to retain this value without having some private variable in my controller? Some nice way that MVC/Razor does it?

like image 357
Pittfall Avatar asked Jun 12 '12 13:06

Pittfall


People also ask

How do you keep data in your ViewBag?

If you want the previous view bag data to be posted, keep that in a hidden form field. After user submit's the form, It will print " From GET-Totally new value "; Try to avoid dynamic stuff like ViewBag/ViewData for transferring data between your action methods and views.

How do you transfer data in ViewBag?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.

Can we store object in ViewBag?

In Controller's Action Method in order to assign the Model data to ViewBag object we assign as shown below. In the below syntax, I am using employee object as well as a list of employee objects to store the data in ViewBag object and return ViewBag to the View or . cshtml and bind it to the HTML table.

Does ViewBag expire?

ViewData/ViewBag - valid only for the duration of the current request. You set it in a controller action and use it in the view, then it disappears.


2 Answers

Option 1:

Pass the value of "ViewBag.Something" to the Controller by using route Values:

@Html.ActionLink("ButtonText", "ActionName", new { Something = @ViewBag.Something })

Option 2: You can use TempData.

public ActionResult Index()
{
 var Something = "YOURVALUE";
 TempData["Something"] = Something;
.......
}


public ActionResult OtherAction()
{
    var Something = TempData["Something "];
    ...........
}

Passing State Between Action Methods

Action methods might have to pass data to another action, such as if an error occurs when a form is being posted, or if the method must redirect to additional methods, as might occur when the user is directed to a login view and then back to the original action method.

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

like image 61
Kapil Khandelwal Avatar answered Oct 20 '22 09:10

Kapil Khandelwal


In the get, set up a model, set it dynamically and when return view() is being executed, do return view(model). Then in the view, set up a hidden field that can keep on passing the value needed. I chose to go this route because I don't have to worry about any server code to make this work on a post and I don't have to worry about any sessions.

like image 22
Pittfall Avatar answered Oct 20 '22 11:10

Pittfall