Reputation: 93
i am trying to use Html.Action() to render partial view(GetNotificationpartial) inside layout.cshtml...but got this error , how can i fix this error
<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>
Upvotes: 1
Views: 3019
Reputation: 1627
In ASP.NET MVC 5, RenderAction Invokes the specified child action method and renders the result inline in the parent view.
@Html.RenderAction ("Home","GetNotificationpartial");
More info in MSDN about RenderAction
But in ASP.NET Core, Analyzer told you If you need to execute code, use a view component instead of a partial view.
You can render the partial view with Partial
Tag helper :
<partial name="_GetNotificationpartial" />
Partial view names begin with an underscore (_) by naming convention.
Also, you can use Async
method to render Partial views :
@await Html.PartialAsync("~/Views/Folder/_GetNotificationpartial.cshtml")
Upvotes: 0
Reputation: 2245
I think you can use ViewComponent or jQuery for this:
Here is a sample use jQuery. If you want to use ViewComponent, I will post a sample later.
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
Upvotes: 2