gpt4 book ai didi

C# 编辑 ViewModel

转载 作者:行者123 更新时间:2023-11-30 16:49:26 31 4
gpt4 key购买 nike

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

View 模型;

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; }
}

Controller ;

[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);
}

最佳答案

我可以建议你再做两个 Action 。

public ActionResult Edit(int id)

您将通过它的 ID 获得 Order,映射到 ViewModel 并将其传递到您将拥有用于编辑的文本框的 View 。使用更新的模型创建另一个 Action 来接受发布请求:

[HttpPost]
public ActionResult Edit(OrderArchiveViewModel model)

提交编辑页面后,您将拥有一个包含新数据的更新模型,然后通过 Id 在数据库中找到您的模型并更新属性。

关于C# 编辑 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36678209/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com