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)
- There is no option for me to create strongly typed view using ASPX engine at all:
- When I create it manually:
Add .aspx file to Views folder like this:
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:*
- I tried as well to use this package from nuget, but to be honest I can't find any documentation about it.
=== ADDITIONAL INFO: ===
System.Web.MVC is present
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:
- I had to add this to my main web.config file, to system.web section
Alternatively add this, to my .aspx View (on the very beginning):
<%@ Import Namespace="System.Web.Mvc.Html"%>
- I had to add this, to my web.config file in 'Views' directory (to system.web section):
Finally in the ASPX View, I had to add ":", like this:
And thank you all so much for quick replies, I really appreciate your help guys.