Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

369
Views
return Views by PartialView method

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:

  1. Is it good way to return a View as PartialView?
  2. How should I set path of views in 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 ?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You should have this

  • MyController

    • ActionResult -> Index
    • ActionResult -> IndexPartial
  • Views

    • My
      • Index.cshtml (View)
      • IndexPartial.cshtml (PartialView)

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).

over 4 years ago · Santiago Trujillo Report

0

you can use :

public PartialViewResult ActionMethodName() 
{
      return PartialView("_Index.chtml");
}

OR

public ActionResult ActionMethodName() 
{
      return PartialView("_Index.chtml");
}
over 4 years ago · Santiago Trujillo Report

0

  1. There is no limitation and it's not consider bad practice.

  2. You have a typo at the PartialView("~/Views/BasicInfo/_Index.chtml") part of your question.

    You should write return PartialView("~/Views/BasicInfo/_Index.cshtml")

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!