Simple ASP.NET MVC

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;

namespace DemoApp
{
	public class Person
	{
		public int Id {get;set;}
		public string Name {get;set;}
	}
	
	public class PersonRepository 
	{
		public IEnumerable<Person> CreateSampleData()
		{
			return new List<Person>()
			{
				new Person{Id=0,Name="Bob"},
				new Person{Id=1, Name = "John"}
			};
		}
	}
}

View

@model IEnumerable<DemoApp.Person>
@{
	Layout = null;
}

<html>
	<head><title> Demo</title></head>
	
	<body>
		<h1> People</h1>
		<ul>
	
			@foreach(var person in Model)
			{
				<li>@person.Name</li>
			}
		</ul>
	</body>
</html>

Controller

using System;
using System.Web.Mvc;
using System.Collections.Generic;

namespace DemoApp
{
	public class HomeController : Controller
	{
		private PersonRepository _personRepository;
		public HomeController()
		{
			_personRepository = new PersonRepository();
		}
		
		[HttpGet]
		public ActionResult Index()
		{
			var data = _personRepository.CreateSampleData();
			return View(data);
		}


		

	}
}
;