I'm trying to convert an ASP.NET MVC project to ASP.NET Core MVC. I have done most of the work but got stuck in a weird issue.
My project has a view User.cshtml
and a layout page UserLayout.cshtml
, based on an API call I was returning the view and its master page through return View("User", "UserLayout");
in ASP.NET MVC.
The method expects a view name and a master name. This overload doesn't exists anymore in ASP.NET Core MVC so I have set the layout on top of my User.cshtml
page like this:
@{ Layout="~/Views/Shared/UserLayout.cshtml"; }
and returned just the view name from my controller.
Now my layout page contains Kendo tabstrip and in its content I'm rendering sections like @RenderSection("HomeTab", true)
which are defined in UserLayout.cshmtl
@section HomeTab {}
. This is working completely fine with the ASP.NET MVC project, but when I run the ASP.NET Core MVC project, I'm getting this exception:
InvalidOperationException: the following sections have been defined but have not been rendered by the page at '/Views/Shared/AgentLayout.cshtml': 'HomeTab'. To ignore an unrendered section call IgnoreSection("sectionName").
I have tried removing the section, changing the required attribute to false but the error remains. It seems I didn't need to convert views from .NET to .NET Core but it's this small thing that has got me stuck.
I found a solution to this posting it here for anyone stuck with the same issue.
I created a razor code block above my kendo tab strip @{} and after the code block closes I added a .Render() function and so far my code is running as expected.
@{
Html.Kendo().TabStrip()
.Name("tabstrip")
.Events(events => events
.Select("onTabSelected")
.Activate("onTabActivated")
.ContentLoad("onTabContentLoaded")
.Error("onTabError")
)
.Animation(animation =>
{
animation.Enable(false);
})
.Items(tabstrip =>
{
if (!ViewBag.isOffline)
{
tabstrip.Add().Text("Home")
.HtmlAttributes(new { id = "hometabid" })
.Selected(true)
.ImageUrl(Url.Content("~/Content/Images/HomeTabIcon.png")).ImageHtmlAttributes(new { id = "hometabimgid" })
.Content(
@<text>
@RenderSection("HomeTab",false)
</text>
).ContentHtmlAttributes(new { @style = "overflow: auto;" });
}
}
}).Render();
}
@RenderBody()