简体   繁体   中英

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

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

//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. So Your PartialView is your method and PartialViewController is your controller.

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.

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)

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

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

I think you are just missing the correct syntax.

You have your partial view: _MyPartialView.cshtml

In your parent html view (can be another view or 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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