I am very confuse with this partial view.
I want to load a partial view inside my main view.
Here is the simple example.
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 .
To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.
The jQuery . load method calls your controller method, passing the value of the search text and updates the contents of the <div> with the partial view. Side note: The use of a <form> tag and @Html. ValidationSummary() and @Html.
If you want to load the partial view directly inside the main view you could use the Html.Action
helper:
@Html.Action("Load", "Home")
or if you don't want to go through the Load action use the HtmlPartialAsync helper:
@await Html.PartialAsync("_LoadView")
If you want to use Ajax.ActionLink
, replace your Html.ActionLink
with:
@Ajax.ActionLink( "load partial view", "Load", "Home", new AjaxOptions { UpdateTargetId = "result" } )
and of course you need to include a holder in your page where the partial will be displayed:
<div id="result"></div>
Also don't forget to include:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
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):
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
I had exactly the same problem as Leniel. I tried fixes suggested here and a dozen other places. The thing that finally worked for me was simply adding
@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval")
to my layout...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With