在我的c# MVC应用程序中循环通过一个视图模型并更新一个值

本文关键字:一个 视图 我的 模型 更新 MVC 循环 应用程序 | 更新日期: 2023-09-27 18:04:00

我有一个视图模型

public class RatesViewModel
{
    public string RoomName { get; set; }
    public string TypeName { get; set; }
    public long TypeID { get; set; }
    public int TypeCount { get; set; }
    public virtual IQueryable<Occ> Occs { get; set; }
}
public class Occ
{
    public string occ { get; set; }
    public decimal ratetocharge { get; set; }
    public int numOfOcc { get; set; }
    public virtual RatesViewModel RatesViewModel { get; set; }
}

填充一个变量"rooms"。

基于某些条件,我想更新Occ。Ratetocharge -例如,将它们全部更改为扣除50:

  foreach (var r in rooms)
        {
        // do a query which looks up a value from another table
        if (conditionMet == true)
        {
             r.Occs.ratetocharge = r.Occs.ratetocharge - 50;
        }
        }
然而,

r.Occs.ratetocharge

…不允许的。我想知道如何更新ratetocharge?是我的视图模型设置错误,还是有更好的方法来做我想要实现的?

谢谢你的建议,

马克

在我的c# MVC应用程序中循环通过一个视图模型并更新一个值

你必须这样做

if (conditionMet == true)
{
    foreach(Occ occ in r.Occs)
    {
        occ.ratetocharge = occ.ratetocharge - 50;
    }
}

你应该使用索引号

  r.Occs[Index].ratetocharge - 50;

或使用loop

foreach (var r in rooms)
        {
        // do a query which looks up a value from another table
        if (conditionMet == true)
           {
       foreach(Occ occ in r.Occs)
           {
         occ.ratetocharge = occ.ratetocharge - 50;
           }       
          }
        }