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 ?
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).
you can use :
public PartialViewResult ActionMethodName()
{
return PartialView("_Index.chtml");
}
OR
public ActionResult ActionMethodName()
{
return PartialView("_Index.chtml");
}
There is no limitation and it's not consider bad practice.
You have a typo at the PartialView("~/Views/BasicInfo/_Index.chtml") part of your question.
You should write return PartialView("~/Views/BasicInfo/_Index.cshtml")