繁体   English   中英

MVC:无法更新我的模型属性之一

[英]MVC: Unable to update one of my model's properties

在我的 mvc 控制器中,我使用的是订单模型。 我的订单模型中的属性之一称为 StatusDesc(状态描述)。 当我最初通过数据库提取此状态时,它会成功填充我的模型,包括此 StatusDesc 属性,但是如果我稍后通过代码更新 StatusDesc,它不会更新,我不知道为什么。 这是我的代码:进入此代码,StatusDesc = "Completed"

我的订单型号:

private string _statusDesc;

    [Display(Name = "Status")]
    public string StatusDesc
    {
        get { return GetOrderHelper.GetOrderStatusDesc(this); }
        set { _statusDesc = value; }
    }

我的 OrderHelper 类包含一个方法来帮助确定订单的状态(已完成、活动或已删除):

public class OrderHelper
{
    public string GetOrderStatusDesc(Order order)
    {
        return order.CompletedDate != null ? "Completed" : (order.Active ? "Active" : "Deleted");
    }
}

在我的控制器中,我尝试更新 Order 模型的 StatusDesc 但是它不会更新。

//Before code is run --> order.StatusDesc = "Completed"
 order.StatusDesc = "Pending";
 //After code is run --> order.StatusDesc = "Completed"

我做错了什么会阻止我的 StatusDesc 更新为“待处理”?

谢谢

因为您正在从其他属性读取 StatusDesc 值。

return order.CompletedDate != null ? "Completed" : (order.Active ? "Active" : "Deleted");

StatusDesc 值基于 CompletedDate 值;

您将返回“已完成”、“活动”和“已删除”。 即使您更新 StatusDesc,它也永远不会返回用于更新它的值。 您可能需要一个条件,说明如果 StatusDesc 不为 null 或为空,则返回 StatusDesc。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM