【问题标题】:Model property value inconsistent模型属性值不一致
【发布时间】:2014-01-11 19:07:18
【问题描述】:

我有一个视图模型,其中包含一个类数组。该模型包含一个名为“isJobber”的布尔值。该模型中的数组根据“isJobber”值(加上更多布尔值)设置更多布尔值,以确定用户是否可以编辑某些特定字段。

我看到一些与可编辑/不可编辑字段不一致的地方,因此我为模型内数组中的每个项目添加了一个“调试”属性。

调试属性有:

debug = isJobber.ToString();

我的模型具有“isJobber”属性。模型返回 true,而调试属性返回 false。

我将 Knockout 与它一起使用,以便能够在 chrome 调试工具中查看所有这些值。

这怎么可能。从用于填充数组的时间到填充到父模型中的时间,isJobber 值永远不会改变。以下是全部代码:

var isJobber = _storeRepo.Query().FirstOrDefault(s => s.OwnerID == ownerId).Jobber;
var goalViewModel = goals.Select(g => new GoalViewModel {
    GoalId = g.GoalId,
    MonthName = GetMonth(g.Month),
    Month = g.Month,
    LYMonthSales = g.LYMonthSalesAdjusted,
    LYMonthSalesActual = g.LYMonthSalesActual,
    CYMonthGoal = g.CYMonthGoal,
    CYMonth = g.CYMonthAdjusted,
    CYMonthActual = g.CYMonthActual,
    ExpoDollars = g.ExpoDollars,
    LYSalesEditable = isJobber && (currentDate.Year < 2014 || (activeMonth == null && currentDate.Month <= g.Month) || activeMonth <= g.Month),
    CYSalesEditable = isJobber && currentDate.Year >= 2014 && activeMonth != null && activeMonth == g.Month,
    GoalEditable = (lastProcessedMonth == null || lastProcessedMonth < g.Month),
    Debug = "currentDate: " + currentDate.ToShortDateString() + "; isJobber: " + isJobber + "; activeMonth: " + activeMonth + "; Month: " + g.Month + "; lastProcessedMonth: " + lastProcessedMonth
}).ToList();

return View(new EarnbackModel {
    EnrollmentId = enrollment.EnrollmentId,
    CustomerName = enrollment.Customer.CustomerName,
    TotalPackagePrice = packages.Where(p => p.CancellationDate == null).Sum(p => p.Price),
    EarnbackCap = enrollment.EarnbackCap ?? 0,
    EarnbackCapMax = isJobber ? 1000000m : 5000m,
    DefaultGoalPercent = 7,
    Ars = arSelections.ToList(),
    Goals = goalViewModel,
    IsJobber = isJobber,
    AvailableStores = stores,
    LastProcessedMonth = lastMonth,
});

以下是 chrome 开发者工具的输出:

viewModel.IsJobber()
true
viewModel.Goals()[0].Debug()
"currentDate: 12/22/2013; isJobber: False; activeMonth: ; Month: 1; lastProcessedMonth: "

我通过 Chrome 开发者工具注意到的一件事,我们托管此网站的服务器认为它是 2013 年 12 月 22 日...不认为这对我的财产价值有任何影响,但值得注意...

回应 PW Cad 的评论/问题:这是淘汰赛的 cshtml 代码。这是否回答了有关 isJobber 可观察的问题?既然它在视图模型中,那不是使它可观察()吗?顺便说一句,我将开始研究 jsFiddle,我以为我以前从未做过。

此外,这在我的开发(本地主机)和测试网站中运行良好。只有生产不起作用...

    var raw = @Html.Json(Model);
    viewModel = ko.mapping.fromJS(raw);

这是 jsFiddle,但如果没有一些实际数据,我不知道如何让它变得有价值......

jsFiddle

这是我的模型:

public class EarnbackModel
    {
        public EarnbackModel()
        {
            Ars = new List<ArSelectionViewModel>();
            Goals = new List<GoalViewModel>();
            AvailableStores = new List<StoreViewModel>();
        }

        public int EnrollmentId { get; set; }
        public string CustomerName { get; set; }
        public decimal TotalPackagePrice { get; set; }
        public decimal EarnbackCap { get; set; }

        public decimal EarnbackCapMax { get; set; }

        public decimal DefaultGoalPercent { get; set; }

        public IList<ArSelectionViewModel> Ars { get; set; }
        public IList<GoalViewModel> Goals { get; set; }

        public bool IsJobber { get; set; }

        public IList<StoreViewModel> AvailableStores { get; set; }

        public NewARViewModel NewAr { get; set; }

        public int LastProcessedMonth { get; set; }
    }

public class GoalViewModel
    {
        public bool MonthClosed { get; set; }

        public int GoalId { get; set; }
        public string MonthName { get; set; }
        public int Month { get; set; }
        public decimal LYMonthSales { get; set; }
        public decimal LYMonthSalesActual { get; set; }
        public decimal CYMonthGoal { get; set; }
        public decimal? CYMonth { get; set; }
        public decimal? CYMonthActual { get; set; }
        public decimal? ExpoDollars { get; set; }

        public bool LYSalesEditable { get; set; }
        public bool CYSalesEditable { get; set; }
        public bool GoalEditable { get; set; }

        public string Debug { get; set; }
    }

【问题讨论】:

  • 您的 isJobber 属性是否可观察?看起来您正在互换使用它。其他东西没有意义 - 如果你 toString 一个布尔值,它应该是“假”而不是“假”,你能创建一个 jsFiddle 来重现你遇到的错误吗?
  • 您只是将 Knockout 用于调试目的吗?如果是这样,我建议改用 Glimpse,因为 Knockout 引入了一个全新的层,它本身就有可能出现错误。 Glimpse 将允许您查看从视图模型返回的属性。如果您只是测试模型的初始状态,这就是要走的路。如果您在从 MVC 控制器返回的模型状态时遇到问题,能否请您也发布您的 C# 模型?
  • 不,页面上有许多填充的字段(来自 GoalViewModel),用户可以在其中编辑和查看其他值的动态变化,这就是使用 Knockout 的原因。
  • @PWKad 我将我的问题编辑为 [try] 并回答您的问题/评论
  • 查看模型转换为 JSON 的页面源代码,您能否验证生成的 JSON 是否包含您想要的初始值。

标签: c# .net asp.net-mvc asp.net-mvc-4 knockout.js


【解决方案1】:

如果它在开发中有效但在生产中无效,那么似乎是发布配置正在更改某些内容,或者生产环境或部署包出现问题。也许尝试打包和重新部署。如果这不起作用,请尝试部署到另一台服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多