find and solve || findandsolve.com
Please wait.....

Get started with ASP.NET Core MVC C#

Get started with ASP.NET Core MVC C#

 

ASP.NET Core MVC is a rich framework for building web application and APIs using the Model-View and Controller design pattern.

What is the MVC pattern?

(MVC) Model-View-Controller architectural pattern separates an application into three main groups of components like  Models, Views, and Controllers.When using this pattern, user requests are routed to first a Controller which is responsible for working on Model to perform user actions and/or retrieve results of queries
The given diagram shows the three main components Model,View,Controller and which ones reference the others:


What is the MVC pattern?

Controller Responsibilities
 

Controller are Interacts with Model and View and also andle user interaction, work with the model, and ultimately select a view to render.The view only displays information in an MVC application.Controller are the mediator of View and Model.
 

"Controllers shouldn't be overly complicated by too many responsibilities. To keep controller logic from becoming overly complex, push business logic out of the controller and into the domain model."
 

Model Responsibilities
 

The Model in an MVC application represents entity class model,the state of the application and any business logic or operations that should be performed by it.In the mode,Business logic should be encapsulated along with any implementation logic for persisting the state of the application.
 

View Responsibilities
 

Views are responsible for presenting content through the user interface and use Razor view engine also you can use .aspx.we can use C# code in view page start with "@" systax.Run time Razor systax .cs code covert into html markup.If you want to need  perform a great deal of logic in view files in order to display data from a complex model and consider the using a View Component, ViewModel, or view template to simplify the view.
 

What is ASP.NET Core MVC
 

This framework is an open source, lightweight and highly testable presentation framework optimized for use with ASP.NET Core.Build dynamic websites and application that enables a clean separation of concerns by patterns-based. Asp.Net core MVC  provides a patterns-based way to build dynamic application. It gives full control over markup and  supports TDD-friendly development also uses the latest web standards.ASP.NET Core Build view page as View.dll.
 

Benefits of using views
 

⦁    The app is easier to maintain because it's better organized. Views are generally grouped by app feature.It is easier to find related views when  working you on a feature.
⦁    The parts of the app are loosely coupled. We can update and build the app's views separately from the business logic and data access components. We can modify the views of the app without necessarily having to update other parts of the app.
⦁    It's easier to test the user interface parts of the app because the views are separate units.
⦁    Due to better organization, it's less likely that you'll accidentally repeat sections of the user interface.
 

For Example Razor view engine

<ul>
    @for (int f = 0; f < 8; f++) {
        <li>@f</li>
    }
</ul>

OR 
@model IEnumerable<Staff>
<ul>
    @foreach (Staff p in Model)
    {
        <li>@p.Name</li>
    }
</ul>
<Staff>:- is the class we can create class inside the Model folder.
@model IEnumerable<Staff> :-This is the ViewModel Path
 

Model binding
 

ASP.NET Core MVC model binding converts client request data (form route data,query string parameters, HTTP headers,values) into objects that the controller can handle it.
 

public async Task<IActionResult> StaffList(StaffViewModel model, string returnUrl = null) { ... }
 

Model validation
 

using System.ComponentModel.DataAnnotations;
public class StaffViewModel
{
    [Required]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    [DataType(DataType.Currency)]
    public string Salary { get; set; }
    [Display(Name = "Join Date")]
    public DateTime JoinDate { get; set; }
  
}

A controller action:

 

public async Task<IActionResult> Satff(StaffViewModel model, string returnUrl = null)
{
    if (ModelState.IsValid)
    {
        // work with the model
    }
    // At this point, something failed, redisplay form
    return View(model);
}

 

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response