Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't viewbag value passing back to the view?

straight forward question , can't seem to get my viewBag value to display in a view that the user is directed to after completing a form.

Please advise..thanks

My Index ActionResult simple returns model data..

public ActionResult Index()
{
    var source = _repository.GetByUserID(_applicationUser.ID);
    var model = new RefModel
    {
        test1 = source.test1,
    };
    return View(model);
}

My Get Edit" ActionResult , simply uses the same model data as Index.

My Post "Edit" ActionResult, assigns the new values if any to the model and redirects to the Index page, but Index page does not display ViewBag value ??

[HttpPost]
public ActionResult Edit(RefModell model)
{
    if (ModelState.IsValid)
    {
        var source = _repository.GetByUserID(_applicationUser.ID);
        if (source == null) return View(model);

        source.test1 = model.test1;
        _uow.SaveChanges();

        @ViewBag.Message = "Profile Updated Successfully";
        return RedirectToAction("Index");      
    }
    return View(model);
}

And in my Index view...

@if(@ViewBag.Message != null)
{
    <div>
        <button type="button">@ViewBag.Message</button>
    </div>
}
like image 914
mkell Avatar asked Oct 10 '13 11:10

mkell


People also ask

How do you pass data from ViewBag to view?

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 use ViewBag to pass data from view to controller?

Yes you cannot pass a Viewbag from view to controller. But you can pass them using TempData.

Why is ViewBag null?

ViewBag doesn't require any type of typecasting for complex data type. ViewBag also has a short life i.e. Its value becomes null when redirection occurs because its life lies only during current request. This is because the aim of ViewBag is to provide a way to transfer/pass data from controllers and views.

How do you know if a ViewBag is empty?

You can check for null and execute your script. This will check that ViewBag. YourKey is null if you want to check it for not null you can change the if condition.


2 Answers

ViewBag only lives for the current request. In your case you are redirecting, so everything you might have stored in the ViewBag will die along wit the current request. Use ViewBag, only if you render a view, not if you intend to redirect.

Use TempData instead:

TempData["Message"] = "Profile Updated Successfully";
return RedirectToAction("Index");

and then in your view:

@if (TempData["Message"] != null)
{
    <div>
        <button type="button">@TempData["Message"]</button>
    </div>
}

Behind the scenes, TempData will use Session but it will automatically evict the record once you read from it. So it's basically used for short-living, one-redirect persistence storage.

Alternatively you could pass it as query string parameter if you don't want to rely on sessions (which is probably what I would do).

like image 87
Darin Dimitrov Avatar answered Oct 01 '22 20:10

Darin Dimitrov


RedirectToAction causes an HTTP 302 response, which makes the client make another call to the server and request a new page.

You should be returning a view instead of redirecting.

like image 42
Natan Avatar answered Oct 01 '22 21:10

Natan