0

I am still learning and today I came to a point when I don't understand why something that should work, actually doesn't.

My sample project, I wanted to test both aspx and razor engine so i created razor view which works like a charm and now it turned out that there is no way for me to add ASPX View to my project and make it work:

(I want to make it work in MVC 5 not MVC 4)

  1. There is no option for me to create strongly typed view using ASPX engine at all:

enter image description here

  1. When I create it manually:

Add .aspx file to Views folder like this:

enter image description here

And put the code that would be generated if I was using MVC 4:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVCDemo.Models.Employee>>" %>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>List</title>
</head>
<body style="font-family:Arial">

    <p>
        <% Html.ActionLink("Create New", "Create") %>
    </p>
    <table class="table" border="1">
        <tr>
            <th>
                <% Html.DisplayNameFor(model => model.Name) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.Gender) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.City) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.Department.Name) %>
            </th>
            <th></th>
        </tr>

        <% foreach (var item in Model) { %>
            <tr>
                <td>
                    <% Html.DisplayFor(modelItem => item.Name) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.Gender) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.City) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.Department.Name) %>
                </td>
                <td>
                    <% Html.ActionLink("Edit", "Edit", new { id = item.Id }) %> |
                    <% Html.ActionLink("Details", "Details", new { id = item.Id }) %> |
                    <% Html.ActionLink("Delete", "Delete", new { id = item.Id }) %>
                </td>
            </tr>
        <% } %>

    </table>
</body>
</html>

and the View is accessed like this:

    public ActionResult Index()
    {
        var employees = db.Employees.Include(e => e.Department);
        return View("List", employees.ToList());
    }

I am getting error, which means basically:

TRANSLATION:

*Server error in Application: '/MVCDemo'.

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Could not load type: 'System.Web.Mvc.ViewPage>'. Source error:*

enter image description here

  1. I tried as well to use this package from nuget, but to be honest I can't find any documentation about it.

enter image description here

=== ADDITIONAL INFO: ===

  1. System.Web.MVC is present

  2. Exactly the same application with Razor View works perfectly fine

I would be really grateful for your help guys.

=== SOLUTION IN CASE SOMEONE HAS SIMILAR PROBLEM ===

So, I managed figure it out, however there is still problem that Visual Studio for new projects support only razor. I wanted to try sth else, Spark for example. But it seems I cannot even import it anymore for MVC 5, I can do it only for MVC 4 project. And creating custom project template would require massive ammount of work I suppose to make it all work together.

Anyway, solution for curious ones:

  1. I had to add this to my main web.config file, to system.web section

enter image description here

Alternatively add this, to my .aspx View (on the very beginning):

<%@ Import Namespace="System.Web.Mvc.Html"%>
  1. I had to add this, to my web.config file in 'Views' directory (to system.web section):

enter image description here

  1. Finally in the ASPX View, I had to add ":", like this:

    enter image description here

And thank you all so much for quick replies, I really appreciate your help guys.

rvnlord
  • 3,487
  • 3
  • 23
  • 32
  • Could you show the error message in English? – Codeman Feb 16 '15 at 20:50
  • Also, have you tried making the "inherits" use a List rather than an IEnumerable? Also make sure that you have tried "rebuild" and not just "build" – Codeman Feb 16 '15 at 20:54
  • Not sure what the, I assume, error message in Polish is saying but double check that the page can resolve the Employee model class. – Ola Ekdahl Feb 16 '15 at 20:57
  • For what it's worth, MVC is compatible with Web Forms to allow you to migrate a Web Forms project to MVC incrementally, while still having the old website function as usual. The intent is not to allow you to just intermingle Web Forms and MVC forever and switch from one to the other at a whim. If you want Web Forms then go with Web Forms, but if you want an MVC project, then go full-hog with MVC. – Chris Pratt Feb 16 '15 at 21:02
  • Translation added, yes I tried using list, I tried rebuild and deleting obj bin folders. Why I cannot add View in .aspx engine? – rvnlord Feb 16 '15 at 21:02
  • Ok, so uhm, why in MVC 4 I could choose engine during View creation and now i can't? I could even import third party engine if I remember correctly. – rvnlord Feb 16 '15 at 21:05
  • Razor is the newer view engine that allows you to write less code that does more. It has been the standard since MVC 3. It is not very likely that anyone would want to create a new project with MVC 5 that uses the .aspx view engine, but there is still support there in case someone is migrating those pages from an older version of MVC. – NightOwl888 Feb 16 '15 at 22:40
  • So what about Spark View engine for example? – rvnlord Feb 16 '15 at 22:42

0 Answers0