Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial view on button click

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?

like image 346
Alen Kopic Avatar asked Aug 25 '14 16:08

Alen Kopic


People also ask

How to render a partial view from a link?

You can render partial view on the load of your view. You just need to hide and show them on click of link. If you dont want to load them initially and want to load them dynalically then you can use AjaxLink to achive it. Let me know if you need any other help. You could try to use JQuery Ajax to load the partial view.

How to render partial view in Parent View in MVC?

You can render the partial view in the parent view using the HTML helper methods: @html.Partial (), @html.RenderPartial (), and @html.RenderAction (). The @Html.Partial () method renders the specified partial view. It accepts partial view name as a string parameter and returns MvcHtmlString.

How to create a partial view with a simple example?

Here we will learn a partial view with a simple example. For that, create the following items in your application. Now we will create a model (CarModel) in our asp.net mvc application for that right click on Models Folder à Select Add à then select Class à now new pop up will open in that select class and give name as CarModel and click Add button.

What is the use of render partial in HTML?

Basically, it stringifies the HTML content in the location where it was specified. The RenderPartial method will not actually return any values or strings and instead, it will write the Partial View that is requested to the Response Stream through response.write internally.


Video Answer


1 Answers

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

like image 176
Ehsan Sajjad Avatar answered Oct 22 '22 08:10

Ehsan Sajjad