I am very confuse with this partial view...
I want to load a partial view inside my main view ...
here is the simple exmaple...
I am loading Index.cshtml of the Homecontroller Index action as a main page..
in index.cshtml I am creating a link via
@Html.ActionLink("load partial view","Load","Home")
in HomeController I am adding a new Action called
public PartialViewResult Load()
{
return PartialView("_LoadView");
}
in _LoadView.cshmtl I am just having a
<div>
Welcome !!
</div>
BUT, when run the project, index.cshtml renders first and shows me the link "Load Partial View" ... when I click on that it goes to new page instade of rendering the welcome message from _LoadView.cshtml into the index.cshtml.
What can be wrong?
Note: I don't want to load page through AJAX or don't want to use Ajax.ActionLink
if you want to populate contents of your partial view inside your view you can use
or
if you want to make server request and process the data and then return partial view to you main view filled with that data you can use
if you want user to click on the link and then populate the data of partial view you can use:
If you want to load the partial view directly inside the main view you could use the
Html.Action
helper:or if you don't want to go through the Load action use the HtmlPartial hepler:
If you want to use
Ajax.ActionLink
, replace yourHtml.ActionLink
with:and of course you need to include a holder in your page where the partial will be displayed:
Also don't forget to include:
in your main view in order to enable
Ajax.*
helpers. And make sure that unobtrusive javascript is enabled in your web.config (it should be by default):For me this worked after I downloaded AJAX Unobtrusive library via NuGet :
Than add in the view the references to jquery and AJAX Unobtrusive:
Next the Ajax ActionLink and the div were we want to render the results:
Small tweek to the above
RenderParital is Better to use for performance.
If you do it with a
@Html.ActionLink()
then loading the PartialView is handled as a normal request when clicking a anchor-element, i.e. load new page with the reponse of the PartialViewResult method.If you want to load it immedialty, then you use
@Html.RenderPartial("_LoadView")
or@Html.RenderAction("Load")
.If you want to do it upon userinteraction (i.e. clicking a link) then you need to use AJAX -->
@Ajax.ActionLink()