I wanted a tip from you who are experienced. I am developing an application where I will use properties of 2 models , which wanted the best option to be used in this case and if possible please kindly list the advantages.
I wanted a tip from you who are experienced. I am developing an application where I will use properties of 2 models , which wanted the best option to be used in this case and if possible please kindly list the advantages.
I've already given a lot of answers about it , but I did not mention the advantages in them, so I'll talk of some.
The approach is widely used when it is not desirable to expose the variables of a Model directly mapped to a database. The ViewModel acts as a data container that is handled by Controller , which can avoid some problems, such as inserting undue data into a database.
In many cases it is desirable for a form to have information that is common or divisible between two or more Models . The ViewModel , in addition to rationalizing the form, also hides the internal information structure.
This is one of my favorites. Imagine a search screen with filters (reposted by a form) and their results.
You can use a form that accepts the verb GET
and accommodate a ViewModel both the search parameters used and the results of that search, in the form of a collection or list. For example:
namespace MeuProjeto.ViewModels
{
public class ProcessoViewModel
{
[DisplayName("Formato de Saída dos Dados")]
public FormatoSaidaDados SaidaDados { get; set; }
[Column("DATA_ENTRADA")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Entrada Inicial")]
public DateTime? DataEntradaInicial { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Entrada Final")]
public DateTime? DataEntradaFinal { get; set; }
[Column("DT_DISTRIB")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Distribuição Inicial")]
public DateTime? DataDistribuicaoInicial { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Distribuição Final")]
public DateTime? DataDistribuicaoFinal { get; set; }
[Column("DATA_ENCERRAMENTO")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Encerramento Inicial")]
public DateTime? DataEncerramentoInicial { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DisplayName("Data de Encerramento Final")]
public DateTime? DataEncerramentoFinal { get; set; }
[Column("PASTA")]
[DisplayName("Caso Inicial")]
public int? PastaInicial { get; set; }
[DisplayName("Caso Final")]
public int? PastaFinal { get; set; }
[Column("COD_CLIENTE")]
[DisplayName("Cliente Inicial")]
public int? ClienteIdInicial { get; set; }
[DisplayName("Cliente Final")]
public int? ClienteIdFinal { get; set; }
[Column("COD_REGIONAL")]
[DisplayName("Código da Regional")]
public String RegionalId { get; set; }
[Column("COD_REGIONAL")]
[DisplayName("Regionais")]
public String[] RegionalIdMultiple { get; set; }
public virtual IEnumerable<Models.Processo> Resultados { get; set; }
}
}
Here is possibly where the purpose of your question best fits. Serialize in JSON using ViewModel is even more advantageous than using Model for three reasons:
Uses only a single command for serialization:
return Json(meuViewModel, JsonRequestBehavior.AllowGet);