Posted on Leave a comment

ASP.NET MVC – First Controller

Visual Studio creates the Controller of Model. Let’s see how.

After creating the Model, Student.cs, in the project, in Visual Studio, position yourself on the Controllers directory. Push the right mouse button and press “Add” -> “Controller”. There appears a mask that offers you to choose the type of controller that you want to create. First on the list it is an empty controller, that is, a class that you have to write independently. The third should be a controller of type “Controller with View, using Entity Framework .” Visual Studio, starting with our Model, creates for us the Controller and view all of the CRUD operations. Choose this type.

Now appears a very important mask. From the top you are asked to choose:

  • Model Class. There is a drop-down menu that is automatically filled with all the classes in the Model (and all the classes in your project). Student choose.
  • Date Class context. You must choose the connection to the database that contains your table. The drop down menu shows all the classes in the project, derived from DbContext class,
  • Check “Generate Views” and “use a Page Layout“.
  • Controller name. Visual Studio there proposes a name one based on the Model you’ve selected. You can change the name, but let the word Controller at the end. (Eg. StudentsController)

Before having to set up the Controller, Visual Studio gives you everything you have written in the models and in DbContext is “right.” If the operation is successful now, in the Controllers directory you find the file StudentsController.cs.

Let’s examine this file. You are inside the class StudentsController derived from Controller.

public class StudentsController : Controller
 private ApplicationDbContext db = new ApplicationDbContext();

// GET: Students
 public ActionResult Index()
 return View(db.Students.ToList());

The class immediately initializes a connection to the database (db).

The first function we see is Index(). It ‘was created automatically and allow us to see the list of our students. Each function of this type is called “Action.” When, in our site we require a list of Students, we launch the Index function of the controller Students according to this path:

The Index function requires, from the connection to the database, all students (db.Students) and turns them into a list (db.Students.ToList ()). At this point sends our list of students to the view (return View (db.Students.ToList ());) As you can see, there isn’t the name of view but only a generic command “View”. The controller  searches in the list of views, for a view  called exactly like the function (Index) and … we see the students.

In the project, in the directory Views, you will find a folder Student with a list of automatically created files (we’ll analyze the view shortly). In the View directory will find the Shared directory. We can say that this directory contains all the “User Controls” ; all interface items that we intend to use in multiple pages; It contains all of the view that are used by other views. Even the one that in the past called “master page” is located here, in the form of “layout”. Look for the _Layout.cshtml file. This is the website of your graphics (we’ll analyze in detail, soon). Find  this piece of code:

<ul class="nav navbar-nav">
 <li>@Html.ActionLink("Home", "Index", "Home")</li>
 <li>@Html.ActionLink("About", "About", "Home")</li>
 <li>@Html.ActionLink("Contact", "Contact", "Home")</li>

These are the buttons you see above, when you launch your site. We need to see a new button to call the student page.

Add to this list html:

<li>@Html.ActionLink("My Student", "Index", "Students")</li>

Then we analyze the ActionLink component.

ActionLink (“Text Displayed”, “Name of Action,” Name of the Controller “). So we will have a button that when pushed will take us to page.

Try running the application and perform a login to test that everything is working.

Leave a Reply

Your email address will not be published. Required fields are marked *