我正在获取一个对象并将其属性写入模型,然后在我的视图中显示模型以进行编辑。之后,用户点击提交并将模型发布回我的控制器。我现在想获取模型的属性并将它们写回对象。
我正在考虑使用反射来做到这一点,但我有点警惕。以为我会检查是否有更明显的答案。
代码:
//OrderDetailsModel
public OrderDetailsModel(Order order)
{
CompletionDateTime = order.CompletionDateTime;
CreateDateTime = order.CreateDateTime;
UpdateDateTime = order.UpdateDateTime;
BusinessArea = order.BusinessArea;
Comment = order.Comment;
CostCenter = order.CostCenter;
CreateUsername = order.CreateUsername;
Customer = order.Customer;
IncidentNumber = order.IncidentNumber;
OrderID = order.ID;
OrderStatus = order.OrderStatus;
ProjectName = order.ProjectName;
ProjectNumber = order.ProjectNumber;
ProjectSubcode = order.ProjectSubcode;
UpdateUsername = order.UpdateUsername;
Description = order.Description;
}
//OrdersController
public ActionResult SaveOrderDetails(OrderDetailsModel orderDetailsModel)
{
Order order = WorkflowManager.GetOrderByID(orderDetailsModel.OrderID);
foreach(PropertyInfo propertyInfo in orderDetailsModel.GetType().GetProperties)
{
//Write properties back.
}
return null;
}
关于如何做到这一点的想法?