WebMatrix - Create controller for CSHTML page - c#

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?

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();
...
}

Related

creating many instances of a webpage from one template?

I'm creating a new website application in asp.net. The landing page needs to have a button (or something similar) which the user can click to create a new instance of a webpage. Similar to how a Facebook user can create a new group/event or a StackOverflow user create a new question.
My website needs to be able to create multiple "events" from the landing page which can then be accessed from the landing page, each event should be a template populated with user details on creation.
Can someone please tell me how people refer to this technique of creating many instances of a webpage (event) from one template?
With ASP.NET Core MVC (using this as an example as you have an ASP.NET tag and your description doesn't specify a technology), you can create a template using a .cshtml file. If you are not familiar with these types of files (which are used within the ASP.NET framework), then I suggest a read of it here:
https://www.w3schools.com/asp/razor_syntax.asp
Roughly, it's a file with HTML content where you can easily embed .NET types (such as types from your Model) and .NET logic using "Razor syntax", so that your HTML file is modified appropriately (e.g. with queried data specific to your user) before being sent back to the client. The reference above gives good examples, so I'm not going to waste space and repeat them here.
You can have certain .cshtml files as your "template" and embed appropriate model data using Razor syntax. You can then have a hyperlink tag (for example) reference the .cshtml file using the asp-action attribute. This will render the .cshtml file to the client whenever that tag is clicked on. ASP.NET uses types called Controllers to handle such requests (Controllers are types that inherit from the Controller type) appropriately, such as querying the correct database and providing your .cshtml file with the correct data before sending the result back to the client.
ASP.NET Core MVC modularizes the types of actions described above very well (M --> Model, V --> View, C --> Controller). Here is a good reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2
For other technologies that you wish to use to achieve the same result, you will have to consult the appropriate references.
Stackoverflow is a place to get answer on specific problem with short answer. Your problem is general design and programming question, and requires understanding of basic programming aproaches.
For that you should grab a book and read about designing webapplications in .Net.

Error with with namespace referencing in asp.net mvc

Hello all am a freshman to MVC 5 and am trying to achieve a project by following a particular video tutorial. In this tutorial I was asked to create folders in Area for Users, Admin, Common and Security. We added BOL using database first Approach. in other to interact with this BOL we added a controller class in Users controller folder and name It index for us to create a get form for submitting details with form. this approach lead to scaffolding template for getting a form. Now at the point of running my project to see the form created an error is being displayed..
My Index.cshtml is as follow:
[Controller][3]
[Folder Arrangement in area][4]

ASP.Net MVC HTMLHelper with external HTML

I saw some Tutorials on ASP.Net MVC HtmlHelpers and they always included the HTML directly into the SourceCode.
I want to create reusable Controls so that I don't have to write a Login view and the parts of it over and over again over the next projects.
The best thing would be if I could write a DLL and place all my created user controls therein
Some time ago I wrote an application with AngularJS and there were directives
and in them was a templateUrl. Is there something similar in Asp.Net MVC ?
I am using the Razor View Engine and the .Net Framework 4.0.
I know I could use partial views but partial views seem to not work in dlls
"The best thing would be if I could write a DLL and place all my created user controls therein" - You can. There is one little cheat which makes it all work really easily.
When you are writing your html helpers, make sure that you change the namespace to System.Web.Mvc.Html.
If you use the TagBuilder class then you shouldn't be using too much html in your C# code.
Then if you reference your dll in the project, you should be able to access the html helper from your razor view
You can use other namespaces, but you have to have to edit the web.config file inside the Views folder and add a reference to the namespace in the <system.web.webPages.razor> section. By re-using the already referenced namespace, you can save yourself some configuration hassles.
Depending on how many projects and how many developers you want to share the code between, you could also consider a build server product (My team used TeamCity for about 2 years before we needed to pay for a licence). You can then produce your own custom NuGet packages, which lets you share (and manage updates) for partial views, editor templates, html helpers and much more.

Is Razor compulsory in ASP.NET MVC 5? [duplicate]

This question already has answers here:
Using ASPX View Engine with MVC 5
(2 answers)
Closed 7 years ago.
I am new to ASP.Net MVC 5 and I want to know if Razor view engine is compulsory, or can you use ASPX view engine?
I try to create new application but I am not getting option to change the view engine.
Razor views are not compulsory. You can use aspx views. When creating your project there is a dropdown that allows you to select which view engine you want the template to use, if you are creating a project from a template.
However, this option doesn't make any difference to your project apart from the views that vs creates in the template. For example, change the name of one of your views, eg. Change home.cshtml to bob.cshtml. Run the project and navigate to the page that uses this view. You will get an error page which displays a list of views that the framework has attempted to find in different folders. This is an operation that uses convention and is the default method mvc will use to find views. Anyway, in the list you will see aspx and cshtml files, so you can go ahead and create an aspx view and mvc will pick it up. Note that the order of the list in the error message is the order mvc will look up each view. It will use the first one it comes across.
Razor views are not compulsory in ASP.net MVC..in ASP.net MVC there are two view Engine are there:
Razor View Engine:
1.Razor View Engine is an advanced view engine and introduced with MVC3. This is not a language but it is a markup syntax.
2.In Razor View Engine we use Layouts.
3.Razor Engine is a little slow compared to Aspx Engine.
Web Form/Aspx View Engine
1.ASPX View Engine is the default view engine for the ASP.NET MVC that is included with ASP.NET MVC from the beginning.
2.In ASPX View Engine we use masterPages.

What is the difference between .aspx and .aspx.cs?

I'm not new to programming but am new to Visual Studio, MVC, C#, ASP.NET, and EXT.NET, i.e. all of the tools I am trying to use.
I am trying to create an Ext.Net 2.0 MVC4 project and was given a similar (in functionality) non-MVC project for reference.
I see that this non-MVC project has .aspx and .aspx.cs files. It seems like the .aspx file basically maps to the "View" in MVC that I want to make... And the .aspx.cs file has the functions that relate to the .aspx file - so is that like the "Controller"? Though the .aspx file also has some functions that seem to not be entirely view-related...
Could someone give me a quick overview or a place to start with this?
ASPX files usually will have the UI and will which is usually HTML tags, some ASP.NET server control embed code (which ultimately produce some HTML markups). ASPX.CS file (usually called "code-behind") will have server-side coding in C#.
If needed, I would relate ASPX page to View and ASPX.CS to Controller action methods.
You have to remember that in webforms, there are ASP.NET controls we will be using in the ASPX file to render some HTML. Examples are TextBox, DataGrid, etc. In MVC, there is nothing called Server control. The View will be pure, handwritten HTML.
If needed, you can create a Hybrid project which is a combination of MVC and webforms. Scott has a post explaining about it here.
No ViewState in MVC :)
When switching from ASP.NET Webforms to MVC, One important thing you have to understand is that MVC architecture tries to stick with the truth that HTTP is stateless. There is no viewstate available in MVC. So you need to make sure that you are repopulating the data in every HTTP Request, as needed. Folks usually run into problems in loading DropDownlist in MVC. There are a lot of answers here in SO about how to handle dropdown lists on postback (when form is posted).
I suggest that you look into some beginner-level tutorials on ASP.NET MVC and start building your app step-by-step, and if you run into any issues, post a (new) question with relevant details.
Good luck, and welcome to the wonderful world of MVC. :)
It sounds like you haven't created an MVC project, but rather a WebForms project.
The *.aspx files are the markup and the *.aspx.cs files are the code-behind files. Code-behind files handle the .NET code for any server-side controls in the *.aspx files.
Checkout Wikipedia's document on ASP.NET, http://en.wikipedia.org/wiki/ASP.NET.
It states:
Web forms are contained in files with a ".aspx" extension; these files
typically contain static (X)HTML markup, as well as markup defining
server-side Web Controls and User Controls where the developers place
all the rc content for the Web page.
Additionally, dynamic code which runs on the server can be placed in a
page within a block <% -- dynamic code -- %>, which is similar to
other Web development technologies such as PHP, JSP, and ASP. With
ASP.NET Framework 2.0, Microsoft introduced a new code-behind model
which allows static text to remain on the .aspx page, while dynamic
code remains in an .aspx.vb or .aspx.cs or .aspx.fs file (depending on
the programming language used).
The .cs file names .aspx.cs is the code behind that goes with .aspx, which generally holds the html, css, javascript and other client side controls.
Generally, dynamic code (C# in this case because of the .cs on the file name) goes in the .cs file as a "good practice" to keep dynamic code and static html separated. Another reason for this abstraction is that the .aspx.cs code is run server side, while the .aspx file is compiled on the server and is then served to the web client requesting it.
Additionally, for MVC, I would suggest using a different view model, specifically Razor, which uses .cshtml files instead of the .aspx.cs and .aspx because they are easier to follow. The reason for the change in MVC is that MVC uses the MVC pattern to abstract layers of code so that .aspx and .aspxcs are not as needed. From a personal experience, I have used both Razor and Webforms (.aspx/.aspx.cs) view models with MVC and I find Razor to be much easier to code/maintain and use.
The aspx file contains your page markup. It's automatically converted into code by ASP.NET.
The cs file contains the code behind your page (initialization, event handlers, etc.). You have to write that code yourself.
These two files are related with the inheritance and he Inherits attribute of the #Page directive associates the page markup to the code behind
.aspx is your markup file. Contains things such as HTML, CSS, JavaScript, and ASP markup.
this .cs file is referred to as a codebehind file. This is where you do thing that may not be available or u are not comfortable doing in scripting languages. Generally aspx is run on the client side while the code behind is executed on the server.

Categories