简体   繁体   English

通过PartialView方法返回视图

[英]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. 我想使用ajax来防止刷新我的页面,为此我想从ajax调用的控制器返回PartialView方法的视图

The questions is: 问题是:

  1. Is it good way to return a View as PartialView ? View作为PartialView返回是一种好方法吗?
  2. How should I set path of views in PartialView method in Controller ? 如何在Controller中的PartialView方法中设置视图路径?

For example for _Index view in Views/BasicInfo/_Index path, I try PartialView("~/Views/BasicInfo/_Index"); 例如,对于Views / BasicInfo / _Index路径中的_Index视图,我尝试PartialView("~/Views/BasicInfo/_Index"); , PartialView("~/Views/BasicInfo/_Index.chtml"); PartialView("~/Views/BasicInfo/_Index.chtml"); , PartialView("BasicInfo/_Index"); 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. 如何将视图名称指定为PartialView方法,如果视图位于共享文件夹之外的文件夹中并且超出相关视图文件夹。 For example My controller is name is controller1 and my View is in this path Views/BasicInfo/_Index ? 例如,我的控制器名称是controller1 ,我的View在此路径中是Views / BasicInfo / _Index

You should have this 你应该有这个

  • MyController myController的

    • ActionResult -> Index ActionResult - >索引
    • ActionResult -> IndexPartial ActionResult - > IndexPartial
  • Views 查看

    • My 我的
      • Index.cshtml (View) Index.cshtml(查看)
      • IndexPartial.cshtml (PartialView) IndexPartial.cshtml(PartialView)

Where: 哪里:

Index.cshtml as complete view will have rendered the partial view with passed model (why so? to prevent code duplication) Index.cshtml作为完整视图将呈现带有传递模型的局部视图(为什么这样?以防止代码重复)

@model YourModelType
@{
    ViewBag.Title = "View Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("IndexPartial", Model)

IndexPartial.cshtml is the partial view, something like IndexPartial.cshtml是局部视图,类似于

@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 现在,当你想要完整的View使用MyController / Index时,如果你想要部分视图,你可以使用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 调用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). 但是你会在通过ajax(不完整或重载的html)将html附加到页面时遇到问题。

you can use : 您可以使用 :

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

OR 要么

public ActionResult ActionMethodName() 
{
      return PartialView("_Index.chtml");
}
  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. 您在PartialView("~/Views/BasicInfo/_Index.chtml")部分有一个拼写错误。

    You should write return PartialView("~/Views/BasicInfo/_Index.cshtml") 你应该写return PartialView("~/Views/BasicInfo/_Index.cshtml")

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)

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(): 这就是我的问题所理解的:你有一个控制器名称Controller1,它有ActionResult someMethod()和另一个控制器BasicInfo,它有一个PartialView BasicInfo_Index,你想从someMethod()返回BasicInfo_Index局部视图:

One you can use the following code in someMethod() to get the partialview of BasicInfo controller : 您可以在someMethod()中使用以下代码来获取BasicInfo控制器的部分视图:

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 或者您可以使用RedirectToAction()在BasicInfo控制器中重新设置动作方法,该方法返回指定的部分视图

What is the return type of the method you're calling in the controller from ajax? 你在ajax控制器中调用的方法的返回类型是什么? It should be something like: 它应该是这样的:

public PartialViewResult LoadPartialView() 
{
      return PartialView("~/Views/BasicInfo/_Index.chtml");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM