Wednesday, March 27, 2019

Creating a Delete view using EF Core

In the previous posts we created a List, Details and Edit views using Asp.Net Core MVC and Entity Framework Core to connect to Database view and edit details from SQL Server DB. In this post we will see on how to create a Delete view which will display the details of an Entity object (User) and allow us to delete the User from the database.

Similar to Edit, the delete operation will use 2 action methods. A GET method to get and display user details and a POST method to delete the user from the DB.

Creating an Edit view and display data using EF Core

In the previous post we saw on how to create a Details view using Asp.Net Core MVC and Entity Framework Core and display the details of an users from the Users table in SQL Server database. In this post we will create an Edit view which will be used to edit the user details.

Remember we had an Edit action link in the list this view will be linked to the Edit link, on clicking the Edit link against a user will trigger the Edit action method in the controller which in turn will get the user details from the DB using Entity Framework Core and pass on the Model object to the View to render the user details in the browser, which can be edited and saved.

Edit and Save will use 2 different action methods a GET and A POST action method, the GET method will get the user details from the DB and the POST method will be used to save the details back to the DB.

Saturday, March 16, 2019

Creating a Detail view and display data using EF Core

In the previous post we saw on how to create a List view using Asp.Net Core MVC and Entity Framework Core and display a list of users from the Users table in SQL Server database. In this post we will create a Details view which will display the user details of a specific user.

Remember we had a Details action link in the list this view will be linked to the Details link, on clicking the Details link against a user will trigger the Details action method in the controller which in turn will get the user details from the DB using Entity Framework Core and pass on the Model object to the View to render the user details in the browser.

Creating a List view and display data using EF Core

In the previous post we created an MVC Controller for our Asp.Net Core application using Entity Framework core. In this post we shall create a List view which will display the list of users in the database fetched using. Let us start by adding a view to our Index action method. Entity Framework Core

Visual studio has already added code in the Index action method of the controller to fetch the list of users using Entity Framework Core.

Thursday, March 14, 2019

Creating a controller for CRUD operation using EF Core

In the previous post we saw on how to setup Entity Framework Core for out Asp.Net Core application. In this post we will use the EFCore and create a controller for CRUD operation. In this sample we will not do too many customization we will rather use the Built in Scaffolding from Visual Studio to create the controller.

In Visual Studio Right-click on the Controllers folder in Solution Explorer and select Add > Controller.

Setting up Entity Framework Core for MVC Database First Application

So far we have seen simple examples using Asp.Net MVC to display static messages and static list of model objects, however complex applications will have to deal with database display and update data from the database. In the previous versions of .Net / MVC we used Entity Framework to interact with the database with .Net Core we will use Entity Framework Core to interact with the database. In this post we shall see on how to set up EF core to interact with the database.

First step to setup EFCore is to create a Models folder, let us create a folder EFCoreModel in the project. Next we need to add the following 3 NuGet packages to the project we can do this either using Package Manager GUI or Package Manager console.

Thursday, March 7, 2019

Tag Helpers in ASP.NET Core

Tag Helpers were introduced in Asp.Net Core, Tag Helpers are similar to HTML Helpers but have improved syntax to look similar to HTML elements. Tag Helpers are attributes added to normal HTML elements to provide server-side processing ability.

Tag Helpers provide IntelliSense support in Visual Studio. There are many built-in Tag Helpers for common tasks such as creating Form, Input, Label, Hyperlink etc

Let us compare the HTML helpers with the new Tag Helpers for a text-box and see the syntax difference.

Tuesday, March 5, 2019

Passing data from Controller to View Examples

In the previous post we saw different ways in which we can pass data / objects from the controller to the View. In this post we will see a simple example on how to assign values in the controller and use them in the View using the different methods ViewData, ViewBag and TempData.

In the controller, we assign string and objects to the ViewData, ViewBag and TempData as follows.

Passing data from Controller to View

There are different ways to pass values / objects from a controller to a view in Asp.Net Core MVC. Following are some of the commonly used ways.

ViewData – Returns a ViewDataDictionary, allows us to store key-value pairs in the controller and access them in the view. The key is a case sensitive string and the value can be any type int/string/object etc. When we use ViewData to pass custom objects we need to cast them in the View before using the properties.

Monday, March 4, 2019

Binding Model to View and Display data

In the previous post we saw on how to create a Model class, in this post we will use the model class in the controller to populate the model properties and pass on a list of Model objects to the view layer to display the data in the view.

In the controller we will instantiate the model class set values to its properties and bind it with the view to display the data. Next let us edit the Home controller and initialize the model object as follows.

Adding Model class to Asp.Net Core MVC

In the previous post we saw a basic Hello Word sample for MVC application using Asp.Net core. Let us now do some more improvement to the example and add a Model class to the project. Model is a Domain object or Business object which contains properties to represent the object. For example Product is a model class which contains properties like ProductName, Number, Cost etc.

Model is one of the layers of the MVC architecture, in MVC model deals with the data / structure of the business objects. Model objects are populated in the controller and passed on to the View layer for display. Let us not add a Model class Product to the MVC project. To do this right click on Model folder -> Add -> click on Class. Let us name our class Product.cs. Add the following code to the Model class.

Friday, March 1, 2019

Asp.Net Core MVC Hello World Example

In the previous post we saw on how to setup a basic Asp.Net core MVC project using Visual studio. In this post we shall examine the various files and methods in the basic project. As a recap we used the following steps to create the basic MVC project.

o   Open Visual studio (I am using VS 2017) and select new Project.
o   Select Web -> .Net Core
o   Select Asp.Net Core web application
o   Provide a name (Hello MVC) and path and click OK
o   In the templates select Web Application (Model-View-Controller) and click OK
o   This will create a basic MVC project with one Controller and View

Now let us examine the files and methods in the project. Let us start with the Program.cs.
This file looks similar to the basic Asp.Net Core project file. It hooks up the host and starts up the application.

Setting up MVC in Asp.Net Core

We have so far seen samples of how to display Hello World text in the browser from Asp.Net core, rendering a static HTML page from Asp.Net core etc. Let us now move on a little further to setup MVC in the Asp.Net Core application. Follow the below steps to create a basic MVC project using Asp.Net Core

Ø  Open Visual studio (I am using VS 2017) and select new Project.
Ø  Select Web -> .Net Core
Ø  Select Asp.Net Core web application
Ø  Provide a name (Hello MVC) and path and click OK
Ø  In the templates select Web Application (Model-View-Controller) and click OK
Ø  This will create a basic MVC project with one Controller and View

The project structure looks like this.