If I understand your question, you will need to have two directories in your project:
- Controllers (will contain the business code for CRUDs);
- .cs files, being one for each Model
- Views (Will display information for each CRUD);
- Subdirectories (one for each Model)
- _CreateOrEdit.cshtml
- Create.cshtml
- Edit.cshtml
- Index.cshtml
- Details.cshtml
- Delete.cshtml
A basic example of CRUD is from a country register (Model Country
). Remember that by rules the Controller name is in the plural, and the subdirectories of the Views as well.
Models \ Country.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MeuProjeto.Models
{
[DisplayColumn("Name")]
public class Country
{
[Key]
public Guid CountryId { get; set; }
[Required]
[Display(Name = "Name", ResourceType = typeof(Resources.Language))]
public String Name { get; set; }
[Required]
[StringLength(3)]
[Display(Name = "Acronym", ResourceType = typeof(Resources.Language))]
public String Acronym { get; set; }
[Display(Name = "States", ResourceType = typeof(Resources.Language))]
public virtual ICollection<State> States { get; set; }
[Display(Name = "LastModified", ResourceType = typeof(Resources.Language))]
public DateTime LastModified { get; set; }
[Display(Name = "CreatedOn", ResourceType = typeof(Resources.Language))]
public DateTime CreatedOn { get; set; }
}
}
Controllers \ CountriesController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MeuProjeto.Models;
using MeuProjeto.Resources;
namespace MeuProjeto.Controllers
{
public class CountriesController : MeuProjetoController
{
private MeuProjetoContext context = new MeuProjetoContext();
//
// GET: /Countries/
[Authorize(Roles = "Administradores")]
public ViewResult Index()
{
return View(context.Countries.Include(country => country.States).ToList());
}
//
// GET: /Countries/Details/5
[Authorize(Roles = "Administradores")]
public ViewResult Details(System.Guid id)
{
Country country = context.Countries.Single(x => x.CountryId == id);
return View(country);
}
//
// GET: /Countries/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Countries/Create
[HttpPost]
[Authorize(Roles = "Administradores")]
public ActionResult Create(Country country)
{
if (ModelState.IsValid)
{
country.CountryId = Guid.NewGuid();
context.Countries.Add(country);
context.SaveChanges();
return RedirectToAction("Index");
}
return View(country);
}
//
// GET: /Countries/Edit/5
[Authorize(Roles = "Administradores")]
public ActionResult Edit(System.Guid id)
{
Country country = context.Countries.Single(x => x.CountryId == id);
return View(country);
}
//
// POST: /Countries/Edit/5
[HttpPost]
[Authorize(Roles = "Administradores")]
public ActionResult Edit(Country country)
{
if (ModelState.IsValid)
{
context.Entry(country).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
return RedirectToAction("Index");
}
return View(country);
}
//
// GET: /Countries/Delete/5
[Authorize(Roles = "Administradores")]
public ActionResult Delete(System.Guid id)
{
Country country = context.Countries.Single(x => x.CountryId == id);
return View(country);
}
//
// POST: /Countries/Delete/5
[HttpPost, ActionName("Delete")]
[Authorize(Roles = "Administradores")]
public ActionResult DeleteConfirmed(System.Guid id)
{
Country country = context.Countries.Single(x => x.CountryId == id);
context.Countries.Remove(country);
context.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing) {
context.Dispose();
}
base.Dispose(disposing);
}
}
}
I do not think you need the codes for each View, but if you need to, just talk.
I do not know if I need to pass another Controller code because they are similar to each other, but if I really need it again, just talk.