13

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 ?

Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56
  • What is your controller name? BasicInfoController? – Serhiy Chupryk Jul 09 '15 at 11:53
  • yes, but is it matter? My be I call **PartialView** from other controller – Siamak Ferdos Jul 09 '15 at 11:55
  • I don't inderstand why you don't want to try to do the way I answered you earlier but it's up to you. You can look [here](http://stackoverflow.com/questions/1371031/asp-net-mvc-partial-view-controller-action), I hope this will help you – Serhiy Chupryk Jul 09 '15 at 13:26
  • **I want use ajax to prevent refresh my pages** please explain what you mean. Or explain another way what you want to accomplish... – Dave Alperovich Jul 09 '15 at 19:10
  • @ Dave Alperovich: I want by clicking on menu, instead of refreshing whole page, I update only a section in my master layout with related view(not partial view). Now the problem is here that if I put views in **Shared** folder it's OK. But in other folder I couldn't set path (I don't know what should I write as path) in **PrtialView** methos – Siamak Ferdos Jul 11 '15 at 03:57

6 Answers6

6

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

SilentTremor
  • 4,747
  • 2
  • 21
  • 34
  • What's view name if I return **PartialView("ViewName", model);** from other controller? – Siamak Ferdos Jul 11 '15 at 04:17
  • Returning different partial view return PartialView("controller/MyViewPartial", model ); but you use current controller. Or redirect to different controller return RedirectToAction("MyController", "GetMyView", model); but if you are and the best approach for post requests is this: return new GetMyView().GetMyView(model); – SilentTremor Jul 13 '15 at 06:36
5

you can use :

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

OR

public ActionResult ActionMethodName() 
{
      return PartialView("_Index.chtml");
}
Ahsan Pervez
  • 262
  • 2
  • 13
4
  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")

Community
  • 1
  • 1
Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46
1

Yea. . Its a best practices to return as Partial View without page load.

 return PartialView("YourPartialView", model)

or else you can use absolute path

return  PartialView("~/Views/AnotherFolder/YourPartialView", model)
er_Yasar
  • 150
  • 10
0

This is what I understood by your question : you have a controller name Controller1 which has ActionResult someMethod() and another controller BasicInfo which has a PartialView BasicInfo_Index and you want to return BasicInfo_Index partial view from someMethod():

One you can use the following code in someMethod() to get the partialview of BasicInfo controller :

var DesirePartialView = ViewEngines.Engines.FindPartialView(ControllerContext, "BasicInfo_Index");
        return PartialView(DesirePartialView.View);

Or You can use RedirectToAction() to redircet to action method in BasicInfo controller which returns the specified partialview

Vignesh Pandi
  • 349
  • 1
  • 4
  • 15
0

What is the return type of the method you're calling in the controller from ajax? It should be something like:

public PartialViewResult LoadPartialView() 
{
      return PartialView("~/Views/BasicInfo/_Index.chtml");
}
kinjaldave
  • 145
  • 1
  • 13