HtmlHelper不包含“动作”的定义

时间:2020-04-17 07:50:16

标签: jquery asp.net-core razor html-helper

我正在尝试使用Html.Action()在layout.cshtml内呈现部分视图(GetNotificationpartial)...但是出现了此错误,我该如何解决该错误

  <li class="dropdown">
                            <a title="الإشعارات" href="#" name="@currenUser.Id" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                <small><span class="glyphicon glyphicon-bell"></span></small><span class="caPet"></span>
                            </a>
                            <ul role="menu" class="dropdown-menu s pre-scrollable" id="notifications">
                                @Html.Action("Home","GetNotificationpartial") 
                                <li><h5 class="text-center">لا توجد إشعارات</h5></li>
                                <li class="divider"></li>
                            </ul>
                        </li>

2 个答案:

答案 0 :(得分:1)

我认为您可以为此使用ViewComponent或jQuery:

这是一个使用jQuery的示例。如果您想使用ViewComponent,我将在稍后发布示例。

1. In layout page

<div id="partialContainer"></div>
<script>   
    $.get('@Url.Action("GetData", "Home")', {id : 1}, function(content){
            $("#partialContainer").html(content);
        });
</script>

2. Home controller 

[HttpGet]
public IActionResult GetData(int id)
{
   return PartialView(id);
}


3. Partial view

@model int 
<span>Values from controler :</span> @Model

答案 1 :(得分:0)

在ASP.NET MVC 5中,RenderAction调用指定的子操作方法,并在父视图中内联呈现结果。

@Html.RenderAction ("Home","GetNotificationpartial");

More info in MSDN about RenderAction

但是在ASP.NET Core中,Analyzer告诉您,如果需要执行代码,请使用视图组件而不是部分视图。

您可以使用Partial标记帮助程序来呈现部分视图:

<partial name="_GetNotificationpartial" />

部分视图名称按照命名约定以下划线(_)开头。

此外,您可以使用Async方法来呈现部分视图:

@await Html.PartialAsync("~/Views/Folder/_GetNotificationpartial.cshtml")
相关问题