I have a scenario wherein I am opening a partial view as modal pop (using bootstrap /jquery) after form submission success /failure cases.
I have tried to pass message for modal pop i.e partialview with tempdata and it works fine.
I am trying to pass messages with viewbag /viewdata. But I am not getting the data in partialview. Is there a way to get the viewbag / viewdata in partial view other than using tempdata.
I am trying to pass messages with viewbag /viewdata. But I am not getting the data in partialview. Is there a way to get the viewbag / viewdata in partial view other than using tempdata.
ViewBag and TempData exist on the web server. The usual cause for this symptom is returning JSON and not a partial view from an AJAX or fetch request. We cannot see you code so it is hard to provide an accurate solution.
Secondly, you have not shared the client code. The other issue we see often on the forum is returning a partial successfully from AJAX but not writing code to update the web page.
I made an example with ajax request, ViewBag has data.I think you need to check the logic of your code to see if a redirect operation has occurred after requesting "CheckModalPopUp".ViewBag only applies to the
current request. If redirection occurs, its value will be null.
Remarks: Please refer to the link about Differences Between ViewData, ViewBag and TempData.
Controller
public ActionResult Index() { return View(); } [HttpPost]
public ActionResult SubmitTest()
{
return View("Index");
} public ActionResult PartialViewTest()
{
ViewBag.NotificationHeader = "Success !";
return PartialView("UserNotifications");
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
43 Posts
Using ViewBag / Viewdata in Partial View
May 27, 2020 05:37 PM|ashvinvee|LINK
Hi,
I have a scenario wherein I am opening a partial view as modal pop (using bootstrap /jquery) after form submission success /failure cases.
I have tried to pass message for modal pop i.e partialview with tempdata and it works fine.
I am trying to pass messages with viewbag /viewdata. But I am not getting the data in partialview. Is there a way to get the viewbag / viewdata in partial view other than using tempdata.
Thanks,
Ashvin
All-Star
53041 Points
23612 Posts
Re: Using ViewBag / Viewdata in Partial View
May 27, 2020 05:50 PM|mgebhard|LINK
ViewBag and TempData exist on the web server. The usual cause for this symptom is returning JSON and not a partial view from an AJAX or fetch request. We cannot see you code so it is hard to provide an accurate solution.
Member
6 Points
43 Posts
Re: Using ViewBag / Viewdata in Partial View
May 27, 2020 06:29 PM|ashvinvee|LINK
Hi mgebhard,
I am sharing the code
Below is the razor syntax for partial view (i.e UserNotifications.cshtml) . I am trying to show Viewbag message in this partial view
This is my Controller code which returns partialview, here is where i am assigning the value for viewbag
public ActionResult CheckModalPopUp() { ViewBag.NotificationHeader = "Success !"; return PartialView("UserNotifications"); }
Thanks,
Ashvin
All-Star
53041 Points
23612 Posts
Re: Using ViewBag / Viewdata in Partial View
May 27, 2020 07:10 PM|mgebhard|LINK
You are not using the ViewBag in the Partial View.
<div class="modal-content"> <div class="modal-body"> <div class="alert alert-dismissible alert-success"> <strong>@ViewBag.NotificationHeader</strong> </div> </div> </div>
Secondly, you have not shared the client code. The other issue we see often on the forum is returning a partial successfully from AJAX but not writing code to update the web page.
Contributor
2710 Points
777 Posts
Re: Using ViewBag / Viewdata in Partial View
May 28, 2020 03:45 AM|YihuiSun|LINK
Hi, ashvinvee
I made an example with ajax request, ViewBag has data.I think you need to check the logic of your code to see if a redirect operation has occurred after requesting "CheckModalPopUp".ViewBag only applies to the current request. If redirection occurs, its value will be null.
Remarks: Please refer to the link about Differences Between ViewData, ViewBag and TempData.
Controller
View
UserNotifications
Here is the result.
Best Regards,
YihuiSun
Member
6 Points
43 Posts
Re: Using ViewBag / Viewdata in Partial View
May 28, 2020 11:44 AM|ashvinvee|LINK
Thanks YihuiSun,
It worked like a charm.
Ashvin