在mvc4中的一个视图中CRUD多个模型 [英] CRUD multiple model in one view in mvc4

查看:97
本文介绍了在mvc4中的一个视图中CRUD多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在一个视图中使用多个模型,并在多个表中添加记录,从查看我的数据库,如下图所示:(我使用vs2012 mvc4和EF)





http://i61.tinypic.com/n17580.png [ ^ ]

i为每个表和类创建四个模型 PreOrder为所有的存储库



i want use multiple model in one view and add records in multiple table from view my db like below image: (i work with vs2012 mvc4 and EF)


http://i61.tinypic.com/n17580.png[^]
i create four model for each table and class "PreOrder" for repository of all

public class Orders
{
    public long OrdersId { get; set; }
    public string CustomerId { get; set; }
    public long OrderListId { get; set; }
    public int? CountProduct { get; set; }
    public string CountPrice { get; set; }
    public string VisitorsName { get; set; }
    public DateTime? OrderDate { get; set; }
}

 public class Product
{
    public string ProductID { get; set; }
    public string NameProduct { get; set; }
    public string Price { get; set; }
}
  public class Customers
{
    public string CustomerID { get; set; }
    [DisplayFormat(NullDisplayText = "-")]
    public string Name { get; set; }
    [DisplayFormat(NullDisplayText = "-")]
    ...
}
public class OrderList
{
    public long OrderListID { get; set; }
    public Nullable<long> OrdersId { get; set; }
    public string ProductId { get; set; }
    public Nullable<int> Count { get; set; }
    public Nullable<decimal> DisCount { get; set; }
}
public class PreOrder
{
    public Customers _Customer { set; get; }
    public Product _Product { set; get; }
    public Orders _Order { set; get; }
    public OrderList _OrderList { set; get; }
  }





i想要使用来自tblcustomers和productId的名称,系列,customerid,NameProduct,来自tblProducts的价格和所有领域tblOrders和tblOrderList如何创建一个视图来填充表Orders和OrderList?



i want use name,family,customerid from tblcustomers and productId,NameProduct,Price from tblProducts and all fields of tblOrders and tblOrderList how can i create one view to fill tables Orders and OrderList??

推荐答案

为此您必须创建ViewModel。然后从您的域模型中填充ViewModel数据并将其绑定到您的视图中(即ViewModel查看)。



什么是ViewModel?



#允许您将一个或多个数据模型或源中的多个实体整形为一个对象



#优化消费和视图渲染



我写了一篇关于此的文章。请查看更多信息。



如何在ASP.NET MVC中使用ViewModel?



For that you have to create ViewModel.Then fill that ViewModel from your domain models data and bind that into your view (i.e. ViewModel to View).

What is a ViewModel ?

# Allow you to shape multiple entities from one or more data models or sources into a single object

# Optimized for consumption and rendering by the view

I have written an article about this.Please check that for more info.

How to Use ViewModel with ASP.NET MVC ?

How to Use ValueInjecter with Asp.net MVC ViewModel ?

/>

这篇关于在mvc4中的一个视图中CRUD多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆