Last updated .
< Previous chapterNext chapter >
I am going to create a simple data-entry app to explore some more features of ASP.Net MVC. I will still be scratching the surface, though, and I will come back to it all in more depth later.
Building on the solution I worked on in the previous chapter I'll make a simple app that allows the user to create a list of his or her contacts. A
MockDatabase.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
using System.Collections.Concurrent;
using MVCTutorial.Models;
namespace MVCTutorial
{
static public class MockDatabase
{
static public ConcurrentBag<Contact> Contacts { get; set; } = new ConcurrentBag<Contact>();
}
}
I am using a collection type from the
I updated the
Controllers/HomeController.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
using Microsoft.AspNet.Mvc;
namespace MVCTutorial.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
ViewData["Title"] = "Contacts";
return View(MockDatabase.Contacts.ToArray());
}
}
}
We need a separate view, in which the user can add the values for a new contact. So, I added a
Views/Contact/Index.cshtml
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
@using MVCTutorial.Models
@model Contact
<!doctype html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<h1>Create a new contact</h1>
@using (Html.BeginForm("AddContact", "Contact")) {
<p>Name: @Html.TextBoxFor(c => c.Name)</p>
<p>Email: @Html.TextBoxFor(c => c.Email)</p>
<input type="submit" value="Save" />
}
</body>
</html>
Here, I am using a number of <input id="Email" name="Email" type="text" value="" />
.
I am also using a <form action="/Contact/AddContact" method="post">
.
Note that the </form>
tag is inserted where
the
So, now we have a page with a form, containing two text boxes and a button to submit the form. The form submits to the
Controllers/ContactController.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
using Microsoft.AspNet.Mvc;
using MVCTutorial.Models;
namespace MVCTutorial.Controllers
{
public class ContactController : Controller
{
public ViewResult Index()
{
ViewData["Title"] = "Add Contact";
return View(new Contact());
}
[HttpPost]
public RedirectToActionResult AddContact(Contact contact)
{
contact.ID = MockDatabase.Contacts.Count + 1;
MockDatabase.Contacts.Add(contact);
return RedirectToAction("Index", "Home");
}
}
}
The controller contains a
There is only a tiny bit missing now: We need a way for the user to navigate from the homepage to the
Views/Home/Index.cshtml
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
@* View for the Home/Index action *@
@using MVCTutorial.Models
@model Contact[]
<!doctype html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<h1>Hello from HomeController.Index !</h1>
<h2>Contacts:</h2>
<ul>
@foreach (Contact contact in Model) {
<li>@contact.Name: @contact.Email</li>
}
</ul>
@Html.ActionLink("Create Contact", "Index", "Contact")
</body>
</html>
The action link specifies that the <a href="/Contact">Create Contact</a>
.
Now, when the user loads up the homepage, there is a link to go to another page to create a new contact:
When the user clicks
A bit of work still remains before this application can go into this production. For one thing, there is no validation; for example it would be relevant to validate that a valid email address is given for a new contact. Also, there is no css styling, so the UI is far from appealing.
< Previous chapterNext : Using client-side libraries >
2016 by Niels Hede Pedersen