C#编辑ViewModel

时间:2016-04-17 15:04:10

标签: c# asp.net-mvc entity-framework code-first

我有我的ViewModel,我的控制器可以从ViewModel正确显示,但是我不确定如何使ViewModel可编辑,以便将编辑后的数据发送回模型。我只想编辑OrderArchiveViewModel,而不是编辑细节

视图模型;

public class OrderArchiveViewModel

    {
        public int OrderId { get; set; }
        public System.DateTime OrderDate { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public decimal Total { get; set; }
        public bool HasBeenShipped { get; set; }
        public List<OrderDetailArchive> Details { get; set; }
    }

    public class OrderDetailArchive
    {
        public string Title { get; set; }
        public string Colour { get; set; }
        public int Quantity { get; set; }
        public decimal UnitPrice { get; set; }
    }

控制器;

[Authorize(Roles = "Administrator")]
    public ActionResult Index()
    {

        List<T_shirt_Company_v3.ViewModels.OrderArchiveViewModel> list = (from o in new TshirtStoreDB().Orders
                            .OrderBy(o => o.OrderDate)
                            .Select(o => new OrderArchiveViewModel()
                            {
                                OrderId = o.OrderId,
                                Address = o.Address,
                                FirstName = o.FirstName,
                                LastName = o.LastName,
                                City = o.City,
                                OrderDate = o.OrderDate,
                                PostalCode = o.PostalCode,
                                Total = o.Total,
                                HasBeenShipped = o.HasBeenShipped,
                                Details = (from d in o.OrderDetails
                                           select new OrderDetailArchive
                                           {
                                               Colour = d.Product.Colour,
                                               Quantity = d.Quantity,
                                               Title = d.Product.Title,
                                               UnitPrice = d.UnitPrice
                                           }).ToList()
                            }).ToList()select o).ToList();

        ViewBag.ShippedMessage = list.Where(w => w.HasBeenShipped).Any() ? "Order has been shipped" : "Order is being processed";

        return View(list);
    }

2 个答案:

答案 0 :(得分:1)

我建议你做另外两个动作。

SELECT t1.hardware, SUM(t1.Quantity), SUM(t2.Quantity) 
FROM request_1v2 AS t1,request_1v2 AS t2 
WHERE t1.date between "2016-03-12" and "2016-03-14" AND t2.date between "2016-03-12" and "2016-03-12" 
GROUP BY t1.hardware

您可以通过它获取SELECT hardware, SUM(t1.Quantity), SUM(t2.Quantity) FROM request_1v2 AS t1,request_1v2 AS t2 WHERE t1.date between "2016-03-12" and "2016-03-14" AND t2.date between "2016-03-12" and "2016-03-12" GROUP BY hardware 的ID,映射到public ActionResult Edit(int id) 并将其传递到您将有文本框进行编辑的视图。使用更新的模型创建另一个用于接受发布请求的操作:

Order

提交编辑页面时,您将获得包含新数据的更新模型,然后按ID在数据库中查找模型并更新属性。

答案 1 :(得分:1)

您可以发送视图代码以获得更多说明吗?

已经给出的答案可以通过重定向到页面进行编辑来完成。

是否要在网格上方显示编辑字段?

为此,您可以添加新的ViewModel,如

public class NewViewModel
{        
    public OrderArchiveViewModel OrderArchiveViewModel { get; set; }
    public List<OrderArchiveViewModel> OrderArchiveViewModelList { get; set; }
}

您可以使用此NewViewModel向View发送数据,其中包含可编辑的OrderArchiveViewModel,具体取决于Id以及OrderArchiveViewModel列表,通过分配Index()操作中的列表。