ASP.NET Core MVC & Razor Pages: Data Binding Samples

Explore practical examples of data binding in ASP.NET Core, covering both MVC and Razor Pages. This documentation showcases various techniques to efficiently transfer data between your client-side and server-side code.

Understanding Data Binding

Data binding is a core concept that simplifies the process of updating the UI based on data and vice versa. In ASP.NET Core, it's fundamental for creating dynamic and interactive web applications.

Key Scenarios:

MVC Data Binding Samples

1. Basic Model Binding with Forms

Demonstrates how form elements automatically bind to properties of a model passed to the controller action.

Controller Example:

public class UserController : Controller
{
    public IActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Register(UserViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Process the valid model data
            return RedirectToAction("Success");
        }
        return View(model); // Redisplay the form with validation errors
    }
}

public class UserViewModel
{
    public string Username { get; set; }
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }
}
            

View Example (.cshtml):

@model UserViewModel

@section Scripts { }

Razor Pages Data Binding Samples

1. Basic Model Binding with Razor Pages

Razor Pages streamline page-focused development. Data binding works similarly to MVC but is integrated directly into the PageModel.

PageModel Example (.cshtml.cs):

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

public class InputModel : PageModel
{
    [BindProperty]
    public string Message { get; set; }

    [BindProperty]
    [Required]
    [EmailAddress]
    public string UserEmail { get; set; }

    public void OnGet()
    {
        // Initialization logic if any
    }

    public IActionResult OnPost()
    {
        if (!ModelState.IsValid)
        {
            return Page(); // Redisplay the page with validation errors
        }

        // Process the bound data (Message, UserEmail)
        return RedirectToPage("./Success", new { email = UserEmail });
    }
}
            

Razor Page Example (.cshtml):

@page
@model InputModel
@{
    ViewData["Title"] = "Input Page";
}

Data Input

@section Scripts { }

Advanced Data Binding Techniques

1. Binding to Collections

Handles scenarios where you need to bind to lists or arrays of data, common in forms for multiple items.

2. Complex Model Binding

Binding to nested objects within your models.

3. Custom Model Binders

For scenarios where default model binding isn't sufficient, allowing you to create custom logic for transforming incoming data.

4. Binding with AJAX

Using JavaScript (e.g., jQuery or Fetch API) to send data to server endpoints and receiving JSON responses.

Explore More Samples on GitHub