I want use ajax to prevent refresh my pages and for this I want return Views by PartialView method from controller on ajax call.
The questions is:
View
as PartialView
?PartialView
method in Controller
? For example for _Index view in Views/BasicInfo/_Index path, I try
PartialView("~/Views/BasicInfo/_Index");
,
PartialView("~/Views/BasicInfo/_Index.chtml");
, PartialView("BasicInfo/_Index");
, and get error as not found the view
EDIT
How specified view name into PartialView method, if view is in a folder out of the Shared folder and out of related view folder. For example My controller is name is controller1 and my View is in this path Views/BasicInfo/_Index ?
How to return a partial view from controller action method? To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.
The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.
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.
A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
You should have this
MyController
Views
Where:
Index.cshtml as complete view will have rendered the partial view with passed model (why so? to prevent code duplication)
@model YourModelType
@{
ViewBag.Title = "View Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.Partial("IndexPartial", Model)
IndexPartial.cshtml is the partial view, something like
@model YourModelType
<h2>Some Title</h2>
<div>
<h4>YourModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Property1)
</dt>
<dd>
@Html.DisplayFor(model => model.Property1)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Property2)
</dt>
<dd>
@Html.DisplayFor(model => model.Property2)
</dd>
</dl>
Now when you want the full View use MyController/Index and when you want Partial View instead you can get it with MyController/IndexPartial
Or you can use parameter on you action to specify the output:
public ActionResult GetMyView(bool? partial)
{
var model = something;
if (partial != null && partial)
{
return PartialView("MyViewPartial", model)
}
return View("MyView", model);
}
call for partial = yourHost/controller/GetMyView?partial=true
Now back to your question, yes you can return Partial View as View and vice versa. But you will face problems in appended html to pages via ajax (incomplete or overloaded html).
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