【问题标题】:Adding data to an existing model将数据添加到现有模型
【发布时间】:2020-11-13 06:28:47
【问题描述】:

我是编码新手。对不起,如果这是一个新手问题,但我有点迷茫。

我有一个模型,它返回一个存储过程并相应地保存到模型中,现在我想稍后将数据添加到模型的字段中。基本上我的模型是一个对象数组,但是当我添加一个新数组时,它会在每个对象上显示整个数组并且在每个对象中都是相同的......即

[0] = Name
    = Country
    = Type 

现在我有另一个属性应该有 CurrentPosition 即动态例如 1 下一个属性应该有 2 等等...

我创建了这段代码

CurrentStep = System.Web.HttpContext.Current.Session["currentStepPosition"];
int stepCount = Convert.ToInt32(CurrentStep);
List<int> stepCountList = new List<int>();
for (int i = 1; i <= stepCount; i++)
{
    stepCountList.Add(i);
}

上面输出一个数字数组,即 1,2,3,4,5 依此类推...

但是,当我为每个对象将它添加到我的模型时,我的模型具有上述属性,但不是为每个属性赋予唯一的 CurrentPosition 即每个对象的 1 等等,它只显示每个对象中的整个数组对象...

我该如何克服这个问题?

###EDIT 包括添加到模型###

    Error = model?.Error == null ? null : new BrokenRule()
    {
        Message = model.Error.Message,
        Name = model.Error.Name,
    };
   
    RecipeStages = model?.Processes?.Select(obj => new GetReleasedRecipeStepsModel()
    {
        StepId = obj.StepId,
        StepType = obj.StepType,
        StepOrder = obj.StepOrder,
        StepName = obj.StepName,
        NumberOfProperties = obj.NumberOfProperties
        //CurrentStepPosition = currentStepConverted
    })?.ToArray() ?? new GetReleasedRecipeStepsModel[] { };

有没有办法迭代模型?添加单个属性数据?在它被推送到视图之前?

【问题讨论】:

  • 能否将代码添加到模型中。错误可能就在那里。

标签: c# model-view-controller model


【解决方案1】:

Select 方法有一个重载,它接收每个项目的索引。因此,您可以使用它来计算您的 CurrentStepPosition,如下所示:

RecipeStages = model?.Processes?.Select((obj, index) => new GetReleasedRecipeStepsModel()
{
    StepId = obj.StepId,
    StepType = obj.StepType,
    StepOrder = obj.StepOrder,
    StepName = obj.StepName,
    NumberOfProperties = obj.NumberOfProperties
    CurrentStepPosition = index + 1
})?.ToArray() ?? new GetReleasedRecipeStepsModel[] { };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多