Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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# Razor中的嵌套Html.Action调用_C#_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# Razor中的嵌套Html.Action调用

C# Razor中的嵌套Html.Action调用,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,编辑:很明显,这是我网站的一个大大简化的版本,如果我制作一个测试应用程序,使用这种模式它工作得很好。在我们真正的应用程序中,我们使用的是T4MVC,这一切都在一个区域内。我猜其中一个因素导致了我的问题 EDIT2:所有默认路由都已定义,如果我直接导航到/AreaName/ControllerName/SubChild?val=123,它将呈现 我对Mvc有一个特殊的问题,我希望有人能帮助我 我有一个具有以下操作方法的控制器 public ActionResult Index() { ret

编辑:很明显,这是我网站的一个大大简化的版本,如果我制作一个测试应用程序,使用这种模式它工作得很好。在我们真正的应用程序中,我们使用的是T4MVC,这一切都在一个区域内。我猜其中一个因素导致了我的问题

EDIT2:所有默认路由都已定义,如果我直接导航到/AreaName/ControllerName/SubChild?val=123,它将呈现

我对Mvc有一个特殊的问题,我希望有人能帮助我

我有一个具有以下操作方法的控制器

public ActionResult Index()
{
   return View(GetModel());
}

public ActionResult Result Child(string blah)
{
   return View(GetModel(blah));
}

public ActionResult Result SubChild(int val)
{
   return View(GetModel(val));
}
然后我有3个视图

Index.cshtml

<div>
@Html.Action("Child", new { blah = "raaa"})
</div>
<div>
    @Html.Action("Child", new { id = "raaa"})
</div>

@Action(“Child”,new{blah=“raaa”})
Child.cshtml

    <div>
@*ERROR HERE*@
    @Html.Action("SubChild", new { val = 123})
    </div>
<h1>@Model.val</h1>
<div>
    @Html.Action("SubChild", new { id = 123})
</div>

@*这里出错*@
@Action(“SubChild”,new{val=123})
SubChild.cshtml

    <div>
@*ERROR HERE*@
    @Html.Action("SubChild", new { val = 123})
    </div>
<h1>@Model.val</h1>
<div>
    @Html.Action("SubChild", new { id = 123})
</div>
@Model.val
当我导航到/I时,抛出一个异常,表示 调用子child操作的Html.Action上的“路由表中没有与提供的值匹配的路由”

这都在同一区域和同一控制器内。如果我更改标记并使用Html.Partial调用子视图(并构造模型并将其传递到视图中),它将呈现良好效果。当我在已经使用Html.Action呈现的视图中调用Html.Action时,问题就出现了

我已经尝试使用 /area/controller/action,在Html.action调用中指定控制器,在路由值和所有这些值的组合中作为参数传递区域


有人知道这可能是什么吗?我假设您可以在使用它呈现的视图中调用Html.Action,我想我可能错了

好的,开箱即用的MVC3有一个名为
id
的默认路由参数。您的
子孩子
操作有一个名为
val
的参数,因此这可能就是问题所在

将操作中的参数重命名为id,或添加新路由

routes.MapRoute(
    "SubChild", 
    "{controller}/SubChild/{val}", 
    new
    {
        controller = "ControllerName",
        action = "SubChild",
        val = UrlParameter.Optional
    }
);

好的,开箱即用的MVC3有一个名为
id
的默认路由参数。您的
子孩子
操作有一个名为
val
的参数,因此这可能就是问题所在

将操作中的参数重命名为id,或添加新路由

routes.MapRoute(
    "SubChild", 
    "{controller}/SubChild/{val}", 
    new
    {
        controller = "ControllerName",
        action = "SubChild",
        val = UrlParameter.Optional
    }
);

您的参数真的命名为
blah
val
?因为通常第一个参数总是被称为
id
。检查
global.asax.cs
中的方法
RegisterRoutes(RouteCollection routes)
。一定有类似的东西

routes.MapRoute("Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
它指示必须如何命名参数

我认为你的行动必须是这样的:

public ActionResult Index()
{
    return View(GetModel());
}

public ActionResult Result Child(string id)
{
    return View(GetModel(id));
}

public ActionResult Result SubChild(int id)
{
    return View(GetModel(id));
}
那么视图中的代码必须是: Index.cshtml

<div>
@Html.Action("Child", new { blah = "raaa"})
</div>
<div>
    @Html.Action("Child", new { id = "raaa"})
</div>

@Action(“Child”,new{id=“raaa”})
Child.cshtml

    <div>
@*ERROR HERE*@
    @Html.Action("SubChild", new { val = 123})
    </div>
<h1>@Model.val</h1>
<div>
    @Html.Action("SubChild", new { id = 123})
</div>

@Action(“SubChild”,新的{id=123})

您的参数真的命名为
blah
val
?因为通常第一个参数总是被称为
id
。检查
global.asax.cs
中的方法
RegisterRoutes(RouteCollection routes)
。一定有类似的东西

routes.MapRoute("Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
它指示必须如何命名参数

我认为你的行动必须是这样的:

public ActionResult Index()
{
    return View(GetModel());
}

public ActionResult Result Child(string id)
{
    return View(GetModel(id));
}

public ActionResult Result SubChild(int id)
{
    return View(GetModel(id));
}
那么视图中的代码必须是: Index.cshtml

<div>
@Html.Action("Child", new { blah = "raaa"})
</div>
<div>
    @Html.Action("Child", new { id = "raaa"})
</div>

@Action(“Child”,new{id=“raaa”})
Child.cshtml

    <div>
@*ERROR HERE*@
    @Html.Action("SubChild", new { val = 123})
    </div>
<h1>@Model.val</h1>
<div>
    @Html.Action("SubChild", new { id = 123})
</div>

@Action(“SubChild”,新的{id=123})

问题似乎与我们的区域和布线设置有关

在第二遍中,我们丢失了routevaluedictionary中对该区域的引用,因此它无法找到正确的路线。在我们登记区域的地方,我们需要登记正确的路线


感谢您在这方面的帮助,我已经对其他答案进行了投票,因为我认为它们可能会在将来帮助其他人。

问题似乎与我们的区域和路由设置有关

在第二遍中,我们丢失了routevaluedictionary中对该区域的引用,因此它无法找到正确的路线。在我们登记区域的地方,我们需要登记正确的路线

谢谢你在这方面的帮助,我对其他答案投了更高的票,因为我认为它们可能会在未来帮助其他人