Why does the value of the viewbag not return to view?

The direct question, it seems, cannot display my viewBag value in the view that the user accesses after the form is completed.

Please advise ... thanks

My ActionResult index simply 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, just uses the same model data as Index.

My Post "Change" ActionResult, assigns new values, if any, and redirects to the Index page, but the ViewBag value is not displayed on the Index page

 [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> } 
+6
source share
4 answers

ViewBag works only for the current request. In your case, you are redirected, so everything that you may have saved in the ViewBag will die with the current request. Use the ViewBag only if you are rendering a view, not if you intend to redirect.

Use TempData :

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

and then, in your opinion:

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

Behind the scenes, TempData will use the session, but it will automatically supplant the record after reading it. Thus, it is mainly used for short stays with one redirection.

Alternatively, you can pass it as a query string parameter if you do not want to rely on sessions (which I will probably do).

+23
source

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

You should return a view instead of a redirect.

+2
source

RedirectToAction ( msdn ) tells your browser a new request.
Thus, your server will be called again, but it will be a new request with an empty bag and all you can do is some kind of internal redirection by simply calling the index method, so the viewbag will still have its own data.

Edit: You will also have to change your index method, or your View line (model) will try to display the edit.
Full code below

 public ActionResult Index() { var source = _repository.GetByUserID(_applicationUser.ID); var model = new RefModel { test1 = source.test1, }; return View("Index",model); } [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 Index(); } return View(model); } 
+2
source

You can also try

controller

 public ActionResult Test() { ViewBag.controllerValue= "testvalue"; .................. } 

View - define the top of the razor page @{string testvalue= (string)ViewBag.controllerValue;}

 $(function () { var val= '@testvalue'; }); 
0
source

Source: https://habr.com/ru/post/955662/


All Articles