soraraso42 / learning-journal

0 stars 0 forks source link

MVC(model view controller) in ASP.net dev #43

Open soraraso42 opened 6 days ago

soraraso42 commented 6 days ago

basically, when a page/app is rendered on the server side, its data == model, DOM == view and logic == controller are in separate folders inside the project.

Transitioning from Blazor to ASP.NET MVC can be a bit of a shift because they follow different paradigms. Here's a basic overview and key differences to help you get started:

1. ASP.NET MVC Overview

ASP.NET MVC (Model-View-Controller) is a server-side web framework that separates application logic into three main components:

Each request in an MVC application follows this pattern:

2. File Structure

In ASP.NET MVC, the file structure is different from Blazor. Here's a typical structure:

3. Razor Syntax (.cshtml Files)

Razor syntax in MVC is similar to Blazor but used differently.

Example of Razor in ASP.NET MVC:

@model YourNamespace.Models.SomeModel
<h2>@Model.Title</h2>
<p>@Model.Description</p>

<form method="post" action="/YourController/Submit">
    <input type="text" name="SomeInput" />
    <button type="submit">Submit</button>
</form>

4. Controllers

A controller in ASP.NET MVC processes incoming requests, interacts with models, and returns views.

Example of a controller:

public class HomeController : Controller
{
    // GET: Home
    public IActionResult Index()
    {
        var model = new SomeModel { Title = "Hello", Description = "Welcome to ASP.NET MVC" };
        return View(model); // Returns the Index.cshtml view
    }

    // POST: Home/Submit
    [HttpPost]
    public IActionResult Submit(SomeModel model)
    {
        // Process the submitted data
        return RedirectToAction("Index");
    }
}

5. Routing

In MVC, URLs are mapped to controller actions via routing. This can be configured in the Startup.cs or Program.cs file.

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

6. Forms and POST Requests

In ASP.NET MVC, form submissions are handled via HTTP POST requests.

Example of form in a view:

<form asp-action="Submit" method="post">
    <input type="text" name="UserName" />
    <button type="submit">Submit</button>
</form>

In the corresponding controller:

[HttpPost]
public IActionResult Submit(string userName)
{
    // Handle form submission
    return View();
}

7. Transition Tips

Next Steps:

  1. Familiarize yourself with Controllers: Learn how controllers interact with models and views.
  2. Understand Razor in MVC: While Razor syntax is the same, the way it's used for rendering server-side views is different from Blazor components.
  3. Work on a simple MVC project: Start with a basic CRUD (Create, Read, Update, Delete) application to understand the MVC flow.

Let me know if you need more details on any part!

soraraso42 commented 6 days ago

the roles of the Views, Controllers, and the role of Program.cs in ASP.NET MVC.

Breakdown:

  1. Views (HTML Structure):

    • Views are where the page structure skeleton (HTML) resides.
    • You define your HTML and UI structure in Razor syntax (.cshtml files) under the Views folder.
    • The views are responsible for rendering the DOM that will be sent to the client.

    Example of a View (Views/Home/Index.cshtml):

    @model YourNamespace.Models.SomeModel
    <h2>@Model.Title</h2>
    <p>@Model.Description</p>
    
    <form asp-action="Submit" method="post">
       <input type="text" name="UserName" />
       <button type="submit">Submit</button>
    </form>
  2. Controllers (Business Logic/Behavior):

    • Controllers handle form validation, processing user inputs, and managing business logic.
    • They don't directly modify the DOM but provide the data and actions that affect how the view is rendered or behaves.
    • Controllers live in the Controllers folder, and each controller is responsible for managing actions related to a specific view or model.

    Example of a Controller (Controllers/HomeController.cs):

    public class HomeController : Controller
    {
       // GET: Home
       public IActionResult Index()
       {
           var model = new SomeModel { Title = "Welcome", Description = "ASP.NET MVC Example" };
           return View(model);  // Renders the Index.cshtml view
       }
    
       // POST: Home/Submit
       [HttpPost]
       public IActionResult Submit(SomeModel model)
       {
           if (ModelState.IsValid)
           {
               // Process form data
               return RedirectToAction("Index");
           }
    
           return View(model); // Reload the view with the validation message if the model is invalid
       }
    }
  3. Program.cs (Application Configuration):

    • Program.cs (or in some projects, Startup.cs) is responsible for configuring the application.
    • This file contains configurations such as routing, middleware, services, and database connections.
    • It doesn’t directly manage the view or controller logic but sets up the environment in which they run.

    Here's where you'd configure things like:

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    
    // Add MVC and routing
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllerRoute(
           name: "default",
           pattern: "{controller=Home}/{action=Index}/{id?}");
    });
    
    app.Run();

Summary:

In essence, the controller decides what data the view should show and handles actions like form submission, while the view is concerned with how that data looks to the user. Let me know if you need more details on any part!