Asp.net MVC tutorial for beginners

In this tutorial we look at the ASP.NET framework and how we can build web applications using Visual Studio and C#. For our MVC tutorials the basic topics are covered and are listed below.

View the tutorial here at wftutorials.com.

  • Introduction
  • Hello World
  • Routing
  • Working with Views
  • Working with layouts
  • Connecting to a database
  • Working with forms

Some samples are shown below of the tutorial content.

Simple Hello World

A simple route called HelloWorld in a controller called HomeController.cs

public IActionResult HelloWorld()
{
    return View();
}

A simple view that goes with the route above.

@{
    ViewData["Title"] = "HelloWorld";
}

<h2>HelloWorld</h2>

The output of this is shown below.

Hello World Sample Route

Working with Database

A route in a controller that connects to a database.

public IActionResult Users()
{
    List<String> emails = new List<String>();
    string connectionString = "server=localhost;user=root;password=;database=wftutorials";
    MySqlConnection conn = new MySqlConnection(connectionString);
    conn.Open();
    var command = new MySqlCommand("SELECT email FROM musers limit 10;", conn);
    var reader = command.ExecuteReader();
    while (reader.Read())
    {
        var value = reader.GetValue(0).ToString();
        emails.Add(value);
        // do something with 'value'
    }
    return View(emails);
}

The View html template that goes with the route above.

@{
    ViewData["Title"] = "Users";
}

@model List<String>;

<h2>Users</h2>

<ul>
    @foreach (var item in @Model)
    {
        <li>@item</li>
    }
</ul>

The output is shown below

List of users

View the full tutorial at wftutorials.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s