Render partial view on button click

Alen Kopic picture Alen Kopic · Aug 25, 2014 · Viewed 31.6k times · Source

so I've tried everything, but I simply can not return a small partial view and place it in div element. This is my parent 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 some alert("message") before .load() function, and it shows a message, but then I put it after .load(), it doesn't show it. I've tried with and without html.raw(). This is my controller action

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

Where am I wrong?

Answer

Ehsan Sajjad picture Ehsan Sajjad · Aug 25, 2014

you don't have to pass complete name of HomeController, In Url.Action() we have to pass prefix of Controller name, when we create controller we have to add a postfix of Controller in mvc and when we use it in helpers we have to just pass Controller name without Controller postfix:

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

}

Do like this:

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

and you should be placing it in respective Views directory of Controller, which is in this case Views -> Home