View partial view at the click of a button

so I tried everything, but I just can't return a small partial view and put it in a div element. This is my parenting view.

<button id="doctors">Doktori</button> <button id="apointments"> Pregledi</button> <div id="someDiv"> </div> <script> document.getElementById("doctors").onclick = function () { myFunction() }; function myFunction() { $("#someDiv").load("@Html.Raw(Url.Action("doctorsview", "HomeController")) ") } </script> 

I put a warning ("message") in front of the .load () function, and it shows the message, but then I put it after .load (), it does not show it. I tried with and without html.raw (). This is my action with the controller

 public ActionResult doctorsview() { return PartialView("~/Views/_Doktor.cshtml", new Doktor()); } 

Where am I mistaken?

+6
source share
1 answer

you don’t need to pass the full name of HomeController , In Url.Action() we need to pass the controller name prefix when we create the controller, we need to add the Controller postfix to mvc and when we use this in helpers, we just need to pass the controller name without Controller postfix:

 public class HomeController: Controller { public ActionResult doctorsview() { return PartialView("~/Views/_Doktor.cshtml", new Doktor()); } } 

Do the following:

 $("#someDiv").load('@Url.Action("doctorsview","Home")'); 

and you have to put it in the appropriate directory of views of Controller, which in this case is Views β†’ Home

+14
source

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


All Articles