Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
AspnetCore MVC - Javscript code for loading parital view is not working

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();          
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

enter image description here

Receive the Ajax and return the Partial View

enter image description here

(Partial View)

enter image description here

Then you can load the Partial View

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!