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

沒有任何明顯的開創性,但每次我運行我的項目時,我收到以下錯誤消息:路徑'/'的控制器未找到或未實現IController

Clearly I'm doing something wrong, could someone please tell me the standard way for rendering a partial view with an associated controller?

顯然,我做錯了,有人可以告訴我使用相關控制器渲染局部視圖的標准方法嗎?

3 个解决方案

#1


2  

Your PartialViewController definition can cause this situation. And your action name is "Index" but you are trying to show "PartialView" function. You can try to use it with "Index" named function and without "Controller" addition:

PartialViewController定義可能會導致這種情況。你的動作名稱是“索引”,但你試圖顯示“PartialView”功能。您可以嘗試將其與“Index”命名函數一起使用,並且不添加“Controller”:

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

#2


0  

You have multiple issues in your code.

您的代碼中存在多個問題。

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

You must define action method and controller method in RenderAction method. So Your PartialView is your method and PartialViewController is your controller.

您必須在RenderAction方法中定義操作方法和控制器方法。所以你的PartialView是你的方法,PartialViewController是你的控制器。

But in server side you don't implement and you don't have any method called partialView. Instead of that you have Index method. Please change that to PartialView like below.

但是在服務器端你沒有實現,你沒有任何名為partialView的方法。而不是你有索引方法。請將其更改為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以便匹配方法名稱和視圖名稱。否則您應該添加您的名稱以返回PartialView(“PartialView”,ds)

And you dont have to give the controller name as "PartialViewController". Omit Controller part and only mention it as PartialView.

而且你不必將控制器名稱作為“PartialViewController”。省略控制器部分,僅將其作為PartialView提及。

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

#3


-2  

 @{  
    Html.RenderPartial("PartialViewExample");  
} 

Why are you using render action?

你為什么使用渲染動作?

Why is your controller called partial view controller?

為什么你的控制器叫部分視圖控制器?

Is there a controller with that name? If so i does it inherit from controller?

是否有一個具有該名稱的控制器?如果是這樣我會繼承控制器嗎?

Use proper naming conventions and keep your partial views inside the controller they are related to.

使用適當的命名約定並將部分視圖保留在與其相關的控制器內。

For example CustomerController

例如CustomerController

I have a partial view called Customer Profile I want to use it over and over again I have an action method that returns that partial view.

我有一個名為Customer Profile的局部視圖我想一遍又一遍地使用它我有一個返回該局部視圖的動作方法。

Then i can call it anywhere.

然后我可以在任何地方調用它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/08/23/72efe8ba659c16ccd3fc9f3262190b7d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com