简体   繁体   English

带控制器的MVC渲染局部视图

[英]MVC Rendering Partial View with controller

I have my main index page and I'm trying to render my partial view with the following code: 我有主索引页面,并且尝试使用以下代码呈现部分视图:

@{Html.RenderAction("PartialView", "PartialViewController");} 

My Controller for it looks like this: 我的控制器看起来像这样:

public ActionResult Index()
{
   GetDataFromProc proc = new GetDataFromProc();
   DataSet ds = proc.CallProcToDataSet("mySproc");
   return PartialView(ds);
}

And my partial view is as follows: 我的部分看法如下:

@using System.Data
@model DataSet

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Year</th>
                <th>Month</th>
                <th>Hits</th>
            </tr>
        </thead>
        <tbody>
            @foreach (DataRow row in Model.Tables[0].Rows)
            {
                <tr>
                    <td>@row["_year"]</td>
                    <td>@row["_monthName"]</td>
                    <td>@row["_monthCount"]</td>
                </tr>
            }
        </tbody>
    </table>
</div>

Nothing overtly groundbreaking, but each time I run my project I get the following error message: The controller for path '/' was not found or does not implement IController 没什么大胆的突破,但是每次我运行我的项目时,都会收到以下错误消息: The controller for path '/' was not found or does not implement IController

Clearly I'm doing something wrong, could someone please tell me the standard way for rendering a partial view with an associated controller? 显然我做错了什么,有人可以告诉我使用关联控制器渲染部分视图的标准方法吗?

Your PartialViewController definition can cause this situation. 您的PartialViewController定义可能会导致这种情况。 And your action name is "Index" but you are trying to show "PartialView" function. 并且您的操作名称是“ Index”,但是您试图显示“ PartialView”功能。 You can try to use it with "Index" named function and without "Controller" addition: 您可以尝试将其与命名为“ Index”的函数一起使用,而无需添加“ Controller”:

//Usage Style: @{Html.RenderAction("ActionName", "ControllerName");} 
@{Html.RenderAction("Index", "PartialView");} 

You have multiple issues in your code. 您的代码中有多个问题。

@{Html.RenderAction("PartialView", "PartialViewController");} 

You must define action method and controller method in RenderAction method. 您必须在RenderAction方法中定义操作方法和控制器方法。 So Your PartialView is your method and PartialViewController is your controller. 因此,您的PartialView是您的方法, PartialViewController是您的控制器。

But in server side you don't implement and you don't have any method called partialView . 但是在服务器端,您没有实现,也没有名为partialView任何方法。 Instead of that you have Index method. 相反,您具有Index方法。 Please change that to PartialView like below. 请将其更改为PartialView如下所示。

public ActionResult PartialView()
{
   GetDataFromProc proc = new GetDataFromProc();
   DataSet ds = proc.CallProcToDataSet("mySproc");
   return PartialView(ds);
}

You must name the view as PartialView inorder to match the method name and view name.otherwise you should add your name to return PartialView("PartialView", ds) 必须将视图命名为PartialView才能匹配方法名称和视图名称。否则,应添加名称以return PartialView("PartialView", ds)

And you dont have to give the controller name as "PartialViewController". 而且您不必指定控制器名称为“ PartialViewController”。 Omit Controller part and only mention it as PartialView . 省略Controller部分,仅将其称为PartialView

@{Html.RenderAction("PartialView", "PartialView");} 

I think you are just missing the correct syntax. 我认为您只是缺少正确的语法。

You have your partial view: _MyPartialView.cshtml 您拥有部分视图: _MyPartialView.cshtml

In your parent html view (can be another view or layout.cshtml): 在您的父html视图中(可以是另一个视图或layout.cshtml):

@Html.Action("MyPartialView", "MyPartialController")

Create a new controller or use an existing controller: 创建一个新的控制器或使用一个现有的控制器:

//
[HttpGet]
public PartialViewResult MyPartialView()
{
    MyPartialViewModel model = new MyPartialViewModel();
    return PartialView("~/Path/To/_myPartialView.cshtml", model);
}

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

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