I have multiple models and I am trying to calculate a parameter named 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).
我有多个模型,我正在尝试计算一个名为total的参数。我是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. The program works like this: each billLine can have 1 article and 1 bill can have many bill lines.
总数是用这3个值计算出来的,但是我不知道怎么算。这个程序是这样工作的:每个billLine可以有一个文章,而一个bill可以有许多bill line。
3 个解决方案
#1
2
As per understanding I have following Code: Considering Quantity
refers to no of Items it should be Int and Price Be a Float property. TotalArticlePrice
is a calculated property.
根据我的理解,我有以下代码:考虑到数量指的是没有项目,它应该是Int,价格是一个浮动属性。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有一篇文章,所以模型应该与文章属性和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.
最后,正如您所提到的,一个账单可能有多个BillLine,因此模型应该如下所示,其中Total property存储了总账单价格。
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.
如果我有什么误解,请在评论中简要说明。
#2
1
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
理解ViewModel ASP。NET MVC
Multiple Models in a View in ASP.NET MVC 4 / MVC 5
在ASP视图中的多个模型。NET MVC 4 / MVC 5
#3
0
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;
}
}
}
#1
2
As per understanding I have following Code: Considering Quantity
refers to no of Items it should be Int and Price Be a Float property. TotalArticlePrice
is a calculated property.
根据我的理解,我有以下代码:考虑到数量指的是没有项目,它应该是Int,价格是一个浮动属性。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有一篇文章,所以模型应该与文章属性和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.
最后,正如您所提到的,一个账单可能有多个BillLine,因此模型应该如下所示,其中Total property存储了总账单价格。
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.
如果我有什么误解,请在评论中简要说明。
#2
1
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
理解ViewModel ASP。NET MVC
Multiple Models in a View in ASP.NET MVC 4 / MVC 5
在ASP视图中的多个模型。NET MVC 4 / MVC 5
#3
0
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;
}
}
}