如何使用ViewModel模式在MVC中编辑多个表

时间:2016-06-06 12:20:33

标签: c# asp.net sql-server asp.net-mvc asp.net-mvc-4

我正在尝试在webgrid中的MVC Web应用程序中执行CURD操作,但问题是我有多个表但不知道如何使用多个表执行EDIT操作。

发票表

public Invoice()
{
    this.LineItems = new HashSet<LineItem>();
}

public int Customer_ID { get; set; }
public string Customer_name { get; set; }
public string Customer_Address { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }

产品表

public Produc()
{
    this.LineItems = new HashSet<LineItem>();
}

public int Product_ID { get; set; }
public string Product_name { get; set; }
public int Unit_Price { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }

LineItems表

public partial class LineItem
{
    public int Customer_ID { get; set; }
    public int LineItems_ID { get; set; }
    public int Product_ID { get; set; }
    public int Quantity { get; set; }
    public int Total { get; set; }

    public virtual Invoice Invoice { get; set; }
    public virtual Produc Produc { get; set; }
}

视图模型

public class ViewModel
{
    public string Customer_name { get; set; }
    public string Customer_Address { get; set; }        
    public int Quantity { get; set; }
    public int Total { get; set; }
    public string Product_name { get; set; }
    public int Unit_Price { get; set; }
}

这是一个将为我执行CURD操作的类

public class Class1
{
    SalesOrderEntities entities = new SalesOrderEntities();

    public bool SaveStudent(ViewModel viewModel)
    {
        try
        {
            var Invoice = new Invoice()
            {
                Customer_name = viewModel.Customer_name,
                Customer_Address = viewModel.Customer_Address
            };
            var LineItem = new LineItem()
            {
                Quantity = viewModel.Quantity,
                Total = viewModel.Total
            };
            var Produc = new Produc()
            {
                Product_name=viewModel.Product_name,
                Unit_Price=viewModel.Unit_Price
            };
            return true;
        }
        catch
        {

            return false;
        }

    }

    public bool UpdateStudent()
    {
        try
        {

        }
        catch (Exception)
        {

            throw;
        }

    }

现在,我有问题,我不知道如何执行编辑功能。

1 个答案:

答案 0 :(得分:3)

使用Entity Framework进行更新可能非常简单,因为它默认支持更改跟踪。更改跟踪将让EF自动管理您的实体拉动后发生的任何更改,以便在您调用SaveChanges()时,将在数据库级别进行相同的更改。

添加新实体的示例

由于您已经拥有了数据上下文,因此在创建新实体时,您只需要确保将它们正确添加到上下文中并在完成后保存更改:

// Add each of your new entities to their appropriate table in the context and then save
// your changes
entities.Invoices.Add(new Invoice(){
    Customer_name = viewModel.Customer_name,
    Customer_Address = viewModel.Customer_Address
});
entities.LineItems.Add(new LineItem(){
    Quantity = viewModel.Quantity,
    Total = viewModel.Total
});
entities.Producs.Add(new Produc(){
    Product_name = viewModel.Product_name,
    Unit_Price = viewModel.Unit_Price
});
// Now save your changes
entities.SaveChanges();

更新现有实体的示例

更新基本上会以相同的方式工作,但是您需要访问标识符,以便查询现有实体,进行更改并保存它们:

public ActionResult UpdateStudent(int studentId)
{
     using(var entities = new SalesOrderEntities())
     {
          // Get your student
          var student = entities.Students.FirstOrDefault(s => s.StudentID == studentId);
          if(student == null)
          {
               // Student wasn't found
               return HttpNotFound();
          }
          // Create a view with the existing student data
          return View(student);
     }

}

[HttpPost]
public bool UpdateStudent(UpdateStudentViewModel viewModel)
{
     try
     {
         using(var entities = new SalesOrderEntities())
         {
              // Retrieve your existing student (or other entities)
              var existingStudent = entities.Students.FirstOrDefault(s => s.StudentID == viewModel.StudentID);
              // Now that you have your entity, update the appropriate properties
              existingStudent.Property = viewModel.Property;
              // Then finally save your changes
              entities.SaveChanges();
         }
     }
     catch(Exception ex)
     {
         // Something went wrong updating the user
     }
}