简体   繁体   English

如何计算多个模型之间的参数? asp.net MVC

[英]How to calculate a parameter between multiple models? asp.net MVC

I have multiple models and I am trying to calculate a parameter named total. 我有多个模型,我正在尝试计算一个名为total的参数。 I am new to MVC and are not really sure how to do this or where (seen that it should not be done in view that's for sure). 我是MVC的新手,并不确定如何做到这一点或在哪里(看到它不应该在视野中完成)。

I have this parameter I have to calculate: 我有这个参数我必须计算:

public class Bill
{
public int Total { get; set; }
}

by using these parameters: 通过使用这些参数:

public class Article
{
   public int Price { get; set; }
   public float Quantity { get; set; }
}

and also: 并且:

public class BillLine
{
   public int DiscountValue { get; set; }
}

The total is calculated using those 3 values but I am not really sure how to do it. 使用这3个值计算总数,但我不确定如何做到这一点。 The program works like this: each billLine can have 1 article and 1 bill can have many bill lines. 该程序的工作方式如下:每个billLine可以有1篇文章,1个帐单可以有很多帐单行。

As per understanding I have following Code: Considering Quantity refers to no of Items it should be Int and Price Be a Float property. 根据我的理解,我有以下代码:考虑Quantity是指没有项目,它应该是Int和Price是浮动属性。 TotalArticlePrice is a calculated property. TotalArticlePrice是计算属性。

public class Article
{
    public float Price { get; set; }
    public int Quantity { get; set; }


    public float TotalArticlePrice
    {
        get
        {
            return Price * Quantity;
        }

    }
}

As you mentioned that one BillLine has one Article so model should be as below with Article property and FinalBillLinePrice which is calculated deducting the discout value 正如你所提到的那样,一条BillLine有一条条款,所以模型应该如下所示, Article属性和FinalBillLinePrice是计算出扣除FinalBillLinePrice的价值

public class BillLine
{
    public int DiscountValue { get; set; }
    public Article Article { get; set; }

    public float FinalBillLinePrice
    {
        get
        {
            return Article.TotalArticlePrice - DiscountValue;
        }

    }
}

finally, As you mentioned One Bill May have Multiple BillLine ,therefore Model should look like below, where Total property stores the Total Bill Price. 最后,正如您所提到的,One Bill可能有多个BillLine ,因此Model应该如下所示, Total属性存储Total Bill Price。

public class Bill
{
    public float Total
    {
        get
        {

            return BillLineList.Sum(x => x.FinalBillLinePrice);

        }

    }
    public List<BillLine> BillLineList { get; set; }
}

In case, I have misunderstood anything Please brief in comments. 如果我误解了任何事情请在评论中说明。

You will need to create a ViewModel by combining all the three classes and it will be easy for you to do all the calculation using this. 您需要通过组合所有三个类来创建ViewModel,并且您可以使用它轻松地进行所有计算。

To get started in Using ViewModel, please refer the links below. 要开始使用ViewModel,请参阅以下链接。

Understanding ViewModel in ASP.NET MVC 了解ASP.NET MVC中的ViewModel

Multiple Models in a View in ASP.NET MVC 4 / MVC 5 ASP.NET MVC 4 / MVC 5中视图中的多个模型

Perhaps your view models should look something like this: 也许你的视图模型应该是这样的:

public class BillLine
{
   public int DiscountValue { get; set; }
   public Article Article {get; set; }
}

public class Bill
{
    public List<BillLine> Lines { get; set;}
    public int Total { get 
    {
       int result;
       foreach var line in Lines {
           //Update result with your calculations 
       }
       return result;
    }
   }
}

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

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