Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda