Good practices for Asp.Net MVC 4 - 5

Asked

Viewed 1,265 times

6

I need to know how to organize my project in the question of CRUD's in case there is more than one Model in my application.

For example, I have the student model and the teacher model.

I create a class of the kind DbContext to manage my database, but however, each model will require its own CRUD’s.

Where will the CRUD code be inserted? No DbContext? In a controller master(one controller for controller's) or in itself controller of each model?

In the answer I ask you to give an example of code for two different models.

1 answer

5


If I understand your doubt, you will need to have two directories in your project:

  • Controllers (will contain the business code for Cruds);
    • Arquivos . Cs, one for each Model
  • Views (Will display the information of 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 default the Controller name is plural, and the Views subdirectories are also.

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; }
    }
}

Countriescontroller.Cs controllers

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 don’t think you need each view’s code, but if you need it, just say the word.

I don’t know if I need to pass another Controller code because they look alike, but if I have to, again, just say the word.

  • That’s a good answer, but you haven’t answered my question. The idea was in case of having more than one model that also needs CRUD’s, would be mounted in the same way however in the controller of this model the CRUD’s of it?

  • That. Each model asks for a controller separately, for organisational reasons.

  • My doubt was about the amount of CRUD’s, but apparently then is the correct... Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.