Sunday, May 12, 2019

Asp.Net Core MVC - Creating an Empty Controller and develop Manually

In the previous sample we saw on how to create a controller in Asp.Net core MVC using the using Template to create all Action Methods for CRUD operation, this was very easy since Visual Studio created us all the Action Methods by default. This is very handy when we want to do CRUD operations, however in some cases this might not come in very handy, we might want to create our own action methods, in such cases we will create an Empty controller without Action methods. In this post we shall see on how to create an Empty controller and build on top of it.

Open Visual Studio, create a simple MVC project as we saw in our previous posts. Once the project is up and running we will add our empty controller. Follow the below steps to create an empty controller.

Right click on the Controllers folder and select Add -> Controller.
In the list of templates select MVC Controller – Empty -> Add
In the next step give a name to the controller, EmployeeController -> Add
A new file EmployeeController.cs will be created inside the Controllers folder with the following code.

namespace HelloMVC.Controllers
{
    public class EmployeeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Notice that this controller just has an Index methods, it does not contain reference to the EF Core context or the Action methods for CRUD operation like what we had in our previous sample. This gives us flexibility to add our own Action Methods but we have to add more code manually for doing that.

Next we will add reference to the EFCore DB context, it’s easy, since we already added the context files to the startup we just have to add a Constructor and reference the context file as follows.

private readonly UserRegistrationContext _context;

public EmployeeController(UserRegistrationContext context)
{
    _context = context;
}

Now we have access to the DB context, we shall now use it in the Index() action method to get a list of Employees and pass it on to the View as follows.

public async Task<IActionResult> Index()
{
    return View(await _context.Employee.ToListAsync());
}

Next we shall add a View to the Action Method, right click on the Action method and select Add View. Just select the Empty template in the view, we will build the view manually.



This will create an empty view file Employee/Index.cshtml

@{
    ViewData["Title"] = "Index";
}
<h1>Index</h1>

We will have to create the View Manually, update the view as follows to get the list of Model objects passed from the controller and display as a table of values.

@model IEnumerable<HelloMVC.Models.EFCoreModel.Employee>
@{
    ViewData["Title"] = "Index";
}
<h1>Index</h1>

<div class="container">
    <div class="row">
        <div class="col-md-2">Id</div>
        <div class="col-md-2">Name</div>
        <div class="col-md-2">Age</div>
        <div class="col-md-2">Phone</div>
        <div class="col-md-2">Actions</div>
    </div>
    @foreach (var item in Model)
    {
        <div class="row">
            <div class="col-md-2">@Html.DisplayFor(modelItem => item.EmployeeId)</div>
            <div class="col-md-2">@Html.DisplayFor(modelItem => item.Name)</div>
            <div class="col-md-2">@Html.DisplayFor(modelItem => item.Age)</div>
            <div class="col-md-2">@Html.DisplayFor(modelItem => item.Phone)</div>
            <div class="col-md-2"></div>
        </div>
    }
</div>

Build and run the project, our custom controller and view are now ready to be rendered.



Search Flipkart Products:
Flipkart.com

No comments: