C# 基于两个类(视图模型和域模型)显示自定义实体
嗨,我正在做一个项目使用asp.NETMVC4.0,与实体框架和linq。。。 所以我有两门课。 客户类别:C# 基于两个类(视图模型和域模型)显示自定义实体,c#,asp.net-mvc-4,entity-framework-5,infragistics,ignite-ui,C#,Asp.net Mvc 4,Entity Framework 5,Infragistics,Ignite Ui,嗨,我正在做一个项目使用asp.NETMVC4.0,与实体框架和linq。。。 所以我有两门课。 客户类别: namespace LicenciamentoMVC.Models { public class Cliente { [Key] public int IDCliente { get; set; } public string Nome { get; set; } public string Morada { get; set; } public
namespace LicenciamentoMVC.Models
{
public class Cliente
{
[Key]
public int IDCliente { get; set; }
public string Nome { get; set; }
public string Morada { get; set; }
public string CPostal { get; set; }
public string Localidade { get; set; }
public string Freguesia { get; set; }
public string Conselho { get; set; }
public string Distrito { get; set; }
public string Pais { get; set; }
public string Telefone { get; set; }
public string Telemovel { get; set; }
public string Fax { get; set; }
public string Email { get; set; }
public string Nif { get; set; }
public string WWW { get; set; }
public string Observacoes { get; set; }
public int IDP { get; set; }
public int IDU { get; set; }
public DateTime TStamp { get; set; }
public int Rem { get; set; }
public String TipoCliente { get; set; }
}
public class ClienteModel
{
private static Cliente entity;
public static IQueryable<Cliente> GetListaClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var customers = from c in db.Clientes
orderby c.IDCliente descending
where c.Rem==0
select c;
return customers.AsQueryable<Cliente>();
}
}
}
假设我想从类Processo查看IDprocesso和DataInserido,从类Cliente查看Nome
我的数据库由两个表Clientes和Processos组成。
我的dbContent类:
public class MvcApplication1Context:DbContext
{
public MvcApplication1Context()
: base("name=MvcApplication1Context")
{
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//}
public DbSet<Cliente> Clientes { get; set; }
public DbSet<Processo> Processos { get; set; }
}
这是可行的,但我想知道这是正确的方法还是有更好的方法
再次感谢,…如果我理解正确的话,这些物体之间没有任何真正的联系。这完全取决于需要显示多少数据 如果要显示相关数据,可以使用razor在视图上执行此操作:
@foreach(var client in Model.Clientes)
{
<h1>@client.Name</h1>
<ul>
<li>Model.Processo.FirstOrDefault(f=> f.IDCliente == client.IDCliente).DataInserido.toString("dd-MM-yyyy")</li>
<li>.... other properties...</li>
</ul>
}
@foreach(Model.Clientes中的var客户端)
{
@客户名称
- Model.Processo.FirstOrDefault(f=>f.IDCliente==client.IDCliente).DataInserido.toString(“dd-MM-yyyy”)
- ……其他财产
}
processo中的IDCliente与cliente中的IDCliente之间存在连接…由于存在冲突问题,我没有使用外键..我将代码更改为IDCliente。通过这种方式,您可以访问propertiesok,但现在问题与igniteui组件有关…该组件接收iqueryable…如果您的组件需要iqueryable,则是,您需要创建一个新的poco并返回该poco的一个新iqueryable。因此,如果我理解,我应该创建一个viewmodel,以接收将在视图中显示的值???
public class ProcessoClienteModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessos()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = from p in db.Processos
from c in db.Clientes
orderby p.IDProcesso descending
where p.IDCliente == c.IDCliente
where p.Rem == 0
select new { p.processoID,p.DataInserido,c.Nome} as IQueryable<ClienteModel>;
return processos;
return processos.AsQueryable<ProcessoCliente>();
}
}
namespace LicenciamentoMVC.ModelsView
{
public class ProcessoCliente
{
public int IDProcesso { get; set; }
public string NomeCliente { get; set; }
public DateTime DataInserido { get; set; }
}
public class ProcessoModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessosClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = (from p in db.Processos
join c in db.Clientes on p.IDCliente equals c.IDCliente
orderby p.IDProcesso descending
select new ProcessoCliente { IDProcesso = p.IDProcesso, NomeCliente = c.Nome, DataInserido = p.DataInserido});
return processos.AsQueryable<ProcessoCliente>();
}
}
}
@model IEnumerable<LicenciamentoMVC.ModelsView.ProcessoCliente>
@using Infragistics.Web.Mvc
@{
ViewBag.Title = "Index";
}
@* render of ignite ui grid *@
@( Html.Infragistics().Grid<LicenciamentoMVC.ModelsView.ProcessoCliente>()
.Caption("Processos")
.ID("grid1")
.DefaultColumnWidth("200px")
.PrimaryKey("IDProcesso")
.Columns(column =>
{
column.For(x =>x.NomeCliente).DataType("string").HeaderText("Nome do Cliente").Width("60%");
column.For(x => x.DataInserido).DataType("DateTine").HeaderText("Data de Criação do Processo").Width("40%");
column.For(x => x.IDProcesso).DataType("int").Width("0%");
})
.Features(features =>
{
features.Paging().PageSize(20).PrevPageLabelText("Previous").NextPageLabelText("NEXT");
features.Sorting().Mode(SortingMode.Single).ColumnSettings(settings =>
{
settings.ColumnSetting().ColumnKey("NomeCliente").AllowSorting(true);
});
features.Selection().MultipleSelection(false).Mode(SelectionMode.Row);
features.Filtering().Mode(FilterMode.Simple);
features.Updating()
.EnableAddRow(false)
.EnableDeleteRow(true)
.EditMode(GridEditMode.None);
})
.DataSourceUrl(Url.Action("ListarProcessos"))
.UpdateUrl(Url.Action("DeleteProcessos"))
.AutofitLastColumn(false)
.Width("100%")
.AutoGenerateColumns(false)
.DataBind()
.Render()
)
[GridDataSourceAction]
public ActionResult ListarProcessos()
{
return View(LicenciamentoMVC.ModelsView.ProcessoModel.GetListaProcessosClientes());
}
@foreach(var client in Model.Clientes)
{
<h1>@client.Name</h1>
<ul>
<li>Model.Processo.FirstOrDefault(f=> f.IDCliente == client.IDCliente).DataInserido.toString("dd-MM-yyyy")</li>
<li>.... other properties...</li>
</ul>
}