Reputation: 2297
I'm working on a website with WebMatrix. I recently asked a question about fetching data from a database. When I had tried writing the data logic within my CSHTML page, I was quickly told that I was in the wrong to do that because I was contradicting the principals of MVC. The problem is, MVC was easy for me in Visual Studio 2010, where I had a view page.aspx
and a controller page.aspx.cs
for each page, but in WebMatrix, I am using the Razor C# syntax, so my pages are only a single page.cshtml
file. When I create a new site or a new file, there is no MVC template, so I have to create the views and controllers myself.
How can I display data in page.cshtml
that is fetched from the database using a controller (would my controller be 'page.cshtml.cs'?)? In other words, how can I separate a pages data logic from its actual content/markup/view?
Upvotes: 2
Views: 1772
Reputation: 1039498
I think you are confusing ASP.NET MVC with WebMatrix WebPages. You are using only WebPages where all the code is inside Razor templates. There are no notions of controllers.
But even in WebMatrix you could separate your data access code in a separate file. Create a special folder called ~/App_Code
and inside this folder you could create .NET classes. For example you could have a DataAccess.cs
file where you could put your data access logic. You could also reference external assemblies that you have created. For example:
@using MyNamespace
@{
DataAccess myClass = new DataAccess();
...
}
Upvotes: 3