I am trying to load different partial views based on the value inside view bag and for that I have tried the following javascript method :
function CheckAction()
{
var cat = document.getElementById('CType').value;
var op = document.getElementById('OType').value;
if (op == "Add")
{
if (cat == "State")
{
@ViewBag.PartialView="_StatePartial";
}
}
}
here is the link to the view(cshtml file) : https://pastebin.com/XZPJBwp4
I have debugged the code and found out that the js code is working properly and viewbag is also getting the data but the only problem is that, the partialview is not being loaded. By default, default partialview is being loaded but once the button is clicked, the partialview should be changed that is not happening. please refer to this (https://pastebin.com/XZPJBwp4) view file to get the better understanding.
Here is the server side code from where the partial view is getting value(Controller Method)
[HttpGet]
public IActionResult DataMgmt()
{
ViewBag.PartialView = "_DefaultDataMgmt";
return View();
}
My suggestion is that: The “ViewBag” is rendered at the same time as the page, so you can’t use this to load Partial View, I suggest you to use Ajax to achieve this function.
Delete the and send Ajax in JavaScript:
Receive the Ajax and return the Partial View
(Partial View)
Then you can load the Partial View