I'm working with the MVC design pattern, I have a three-layer architecture, is it necessary to put the models in the Models folder ?, can the models go in another project?
I'm working with the MVC design pattern, I have a three-layer architecture, is it necessary to put the models in the Models folder ?, can the models go in another project?
I am currently working on a system of more layers, but to work with the models I have the same elements in both the Models folder of the web project and in the model project. I'll give you an example.
namespace ProyModelo
public class ProyModPersona {
public int Id { get; set;}
public string Nombre { get; set;}
public bool Activo { get; set;}
}
namespace ProyWeb.Models
public class PersonaModel {
public int Id;
public string Nombre;
public bool Activo;
}
In essence they are the same, even if you need to use tags that only work in your frontend, you can apply them without problem since the only condition that is needed is that the properties have the same name and data type.
Now, there is something important that you must use to work correctly when handling the data flow. There is a tool you can use that is called AutoMapper, which helps you to match your data models; in this case, your ProyModelo models with those of ProyWeb.Models. I usually do this procedure in the controller cs.
AutoMapper can be installed from the same Visual Nuget Package.
For example, if I wanted to psara an object from the frontend to the backend I would do a conversion like:
PersonaModel objPersonaModel = ObjectMapper.Instance.Convert<ProyModPersona , PersonaModel >(objModPersona);
And it also works must be done from backend to frontend. The good thing about all this is that it works with all kinds of data structures, call lists, ViewModels, etc.
I hope this resolves your problem.
You can use a model from anywhere.
Make sure that your MVC project has a reference to the other project that contains the models.
Also make sure that the controller and the view have imported the namespace of the models in the other project.
With new project in visual studio - Aspx MvC 2 (It should work for any version, the same idea)
The solution
the controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClassLibrary1;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult Test()
{
TestModel model = new TestModel();
model.Name = "This is Testing!";
return View(model);
}
}
}
The model of the other ClassLibrary1 project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class TestModel
{
public string Name { get; set; }
}
}
The view
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="ClassLibrary1" %>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
<%= (((TestModel)Model).Name) %>
</asp:Content>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= (((TestModel)Model).Name) %></h2>
<p>
Put content here.
</p>
</asp:Content>