Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未调用MVC局部视图控制器方法_C#_Asp.net Mvc - Fatal编程技术网

C# 未调用MVC局部视图控制器方法

C# 未调用MVC局部视图控制器方法,c#,asp.net-mvc,C#,Asp.net Mvc,我试图调用一个返回部分视图的控制器方法。但是,由于在没有控制器的情况下调用该视图,因此该视图返回空引用 如果我想通过控制器方法调用局部视图,我该怎么做 到目前为止,我的代码如下所示: Layout.cshtml CategoryController.cs _Menu.cshtml 您可以尝试@Html.ActionMenu,您的控制器代替@Html.Partial.Html.Partial加载Html视图,但是Html.Action调用控制器操作并加载响应 <div class="co

我试图调用一个返回部分视图的控制器方法。但是,由于在没有控制器的情况下调用该视图,因此该视图返回空引用

如果我想通过控制器方法调用局部视图,我该怎么做

到目前为止,我的代码如下所示:

Layout.cshtml

CategoryController.cs

_Menu.cshtml


您可以尝试@Html.ActionMenu,您的控制器代替@Html.Partial.Html.Partial加载Html视图,但是Html.Action调用控制器操作并加载响应
<div class="container" id="menu">
     @Html.Partial("~/Views/Category/_Menu.cshtml")
</div>
public ActionResult Menu()
    {
        var model = new CategoryModel()
        {
            categories = new SortedDictionary<int, List<CategoryDTO>>()
        };

        model.categories[0] = new List<CategoryDTO>();
        model.categories[0].Add(new CategoryDTO()
        {
            id = 1,
            name = "Nålebinding"
        });
        model.categories[0].Add(new CategoryDTO()
        {
            id = 2,
            name = "Strik"
        });
        model.categories[0].Add(new CategoryDTO()
        {
            id = 3,
            name = "Hækling"
        });

        model.categories[1] = new List<CategoryDTO>();
        model.categories[1].Add(new CategoryDTO()
        {
            id = 4,
            name = "Sokker"
        });
        model.categories[1].Add(new CategoryDTO()
        {
            id = 5,
            name = "Vanter"
        });

        model.categories[2] = new List<CategoryDTO>();
        model.categories[2].Add(new CategoryDTO()
        {
            id = 6,
            name = "Sokker"
        });

        return PartialView("_Menu", model);
    }
@model GUI.Models.CategoryModel
@using DTO;

@helper subCategories(SortedDictionary<int, List<CategoryDTO>> list, int parent)
{
<ul>
    @foreach(var e in list[parent])
    {
        <li>
            @e.name

            @subCategories(list, e.id)
        </li>
    }
</ul>
}

@subCategories(Model.categories, 0)