Before we get into this post, I first want to state I fully understand that both Visual Studio 2010 and the ASP.NET MVC out of band installer are beta products, so this issue may be fixed, but i wanted to put it out there to see if anyone else has encountered a similar problem. This is an open ended post as I am searching for answers on this issue, but will continue to research this as I have time. If you have any suggestions or a fix drop me a line and we can chat.
The Problem: When creating a out of the box MVC application and adding a custom class to the model folder, trying to reference that data class within a view causes Visual Studio 2010 and the VisualStudio Web Server to crash. The same application works fine with Visual Studio 2008.
Lets show an example.
Visual Studio 2008
In Visual Studio 2008 let create a new MVC application. Create or dont create the test cases, it doesnt really matter.
Once the application is created, Lets add a class to our model folder, with the following definition.
1: public class TestFolder
2: {
3: public string Name { get; set; }
4: }
Now, Lets go ahead and create a new controller from the VS UI. Right click on the Controllers folder and select Add->Controller. Let call this controller TestContoller so we can access the page from a new URL.
In this new controller class we need to create an instance of our TestFolder class so that the view can traverse it. Here is the code for the controller class.
1: public class TestController : Controller
2: {
3: //
4: // GET: /Test/
5: List<Models.TestFolder> myFolder;
6: public ActionResult Index()
7: {
8: myFolder = new List<MvcApplication2.Models.TestFolder>();
9: myFolder.Add(new MvcApplication2.Models.TestFolder { Name="Folder one" });
10: myFolder.Add(new MvcApplication2.Models.TestFolder { Name = "Folder two" });
11: myFolder.Add(new MvcApplication2.Models.TestFolder { Name = "Folder three" });
12: myFolder.Add(new MvcApplication2.Models.TestFolder { Name = "Folder four" });
13: //Response.Write("<h1>Test Controller PAGE</h1>");
14: return View("Test", myFolder);
15: }
Now, lets add a view, right click anywhere in the controller class we have just created and AddView. We need to make sure that we select the strongly typed view (our TestFolder class ) and select the list as the view content.
Here is the View Code
1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication2.Models.TestFolder>>" %>
2:
3: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
4: Test
5: </asp:Content>
6:
7: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
8:
9: <h2>Test</h2>
10: <%foreach (var folder in Model) { %>
11: <h1><%= folder.Name %></h1>
12: <%} %>
13: </asp:Content>
Now, we can run the application and navigate to the http://servername/Test controller and you should see the following.
All is working fine, the MVC app
Visual Studio 2010
If we follow the exact same scenario above but ue Visual Studio 2010 with the MVC extensions and we run it in debug mode we get the following errors.
Visual Studio 2010 crashes and restarts.
The Visual Studio Development Web Server crashes as well, but the following screenshots.
In addition to this these are logged in the Windows event viewer.
This is an open ended post as I am searching for answers on this issue, but will continue to research this as I have time. If you have any suggestions or a fix drop me a line and we can chat.