Home ascx working, cshtml not: content controls
Reply: 2

ascx working, cshtml not: content controls

Chris Published in 2018-01-10 07:19:02Z

This is the ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<NavigationEntryViewModel>>" %>
<%@ Import Namespace="WebShop.Models.ViewModels"  %>

<%  if(Model.Count() > 0){ %>
    <ul class="navigation">
        <%  foreach(NavigationEntryViewModel entry in Model)
                Html.RenderPartial("Entry", entry);
<%  } %>

And this is the new cshtml:

@using WebShop.Models.ViewModels

@model List<NavigationEntryViewModel>

@if(Model.Count() > 0)
    <ul class="navigation">
        @foreach(NavigationEntryViewModel entry in Model)
            Html.RenderPartial("Entry", entry);

Running the project with ascx is fine, but when I run it with the cshtml I get the error

content controls are allowed only in content page that references a master page

I tried to set the Layout to null, that didn´t work. I tried to comment Html.RenderPartial, that didn´t work too.

The View is invoked by

return View("Show", entries);

where entries is a list of NavigationEntryViewModel.

I´m lost, can somone please help?

Vladimir Reply to 2018-01-10 07:50:06Z

Your project seems to have both ASPx view engine pages and Razor view engine views. While MVC allow mixing them, all situations where I saw such approaches caused additional problems. In any way, if you want to use both view engine in your project, follow the recommendations provided in:

ASP.NET MVC 3: Using multiple view engines in same project

Combining ASP.Net MVC 3 Razor and ASPX View Engines in One Project

Chris Reply to 2018-01-17 06:47:13Z

The solution to this is quite simple...

I converted all files to cshtml, renaming the old ones. The problem was, that the "old" viewengine still tried to load some files.

I don´t know exactly why, but after disabling it, it works fine. So, when switching from aspx to cshtml, disable the old viewengine as quickly as possible to avoid those nasty errors.

Thank you, Vladimir.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.451285 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO