Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return Views by PartialView method

Tags:

c#

asp.net-mvc

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 ?

like image 462
Siamak Ferdos Avatar asked Jul 07 '15 11:07

Siamak Ferdos


People also ask

How do I return a partial view from the action method?

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.

What does return View () in MVC do?

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.

How do you call a partial view from another view?

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.

What is PartialView?

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.


1 Answers

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

like image 135
SilentTremor Avatar answered Sep 21 '22 02:09

SilentTremor