如何计算多个模型之间的参数? asp.netMVC
How to calculate a parameter between multiple models? asp.net MVC
我有多个模型,我正在尝试计算一个名为总计的参数。我是 MVC 的新手,不太确定如何执行此操作或在何处执行此操作(看到它不应该这样做,这是肯定的)。
我有这个参数需要计算:
public class Bill
{
public int Total { get; set; }
}
通过使用这些参数:
public class Article
{
public int Price { get; set; }
public float Quantity { get; set; }
}
还有:
public class BillLine
{
public int DiscountValue { get; set; }
}
总数是使用这 3 个值计算的,但我不太确定如何计算。该程序是这样工作的:每个billLine可以有1篇文章,1张账单可以有很多账单行。
您需要通过组合所有三个 类 来创建一个 ViewModel,使用它可以轻松完成所有计算。
要开始使用 ViewModel,请参考以下链接。
也许您的视图模型应该如下所示:
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;
}
}
}
根据我的理解,我有以下代码:考虑到 Quantity
指的是没有项目,它应该是 Int 和 Price 是一个浮点数 属性。 TotalArticlePrice
是计算出来的 属性.
public class Article
{
public float Price { get; set; }
public int Quantity { get; set; }
public float TotalArticlePrice
{
get
{
return Price * Quantity;
}
}
}
正如您提到的,一个 BillLine 有一篇文章,因此模型应如下所示,其中 Article
属性 和 FinalBillLinePrice
是在扣除折扣值
后计算得出的
public class BillLine
{
public int DiscountValue { get; set; }
public Article Article { get; set; }
public float FinalBillLinePrice
{
get
{
return Article.TotalArticlePrice - DiscountValue;
}
}
}
最后,
正如您提到的,一个 Bill
可能有多个 BillLine
,因此模型应如下所示,其中 Total
属性 存储总账单价格。
public class Bill
{
public float Total
{
get
{
return BillLineList.Sum(x => x.FinalBillLinePrice);
}
}
public List<BillLine> BillLineList { get; set; }
}
以防万一,我有什么误解,请在评论中简要说明。
我有多个模型,我正在尝试计算一个名为总计的参数。我是 MVC 的新手,不太确定如何执行此操作或在何处执行此操作(看到它不应该这样做,这是肯定的)。
我有这个参数需要计算:
public class Bill
{
public int Total { get; set; }
}
通过使用这些参数:
public class Article
{
public int Price { get; set; }
public float Quantity { get; set; }
}
还有:
public class BillLine
{
public int DiscountValue { get; set; }
}
总数是使用这 3 个值计算的,但我不太确定如何计算。该程序是这样工作的:每个billLine可以有1篇文章,1张账单可以有很多账单行。
您需要通过组合所有三个 类 来创建一个 ViewModel,使用它可以轻松完成所有计算。
要开始使用 ViewModel,请参考以下链接。
也许您的视图模型应该如下所示:
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;
}
}
}
根据我的理解,我有以下代码:考虑到 Quantity
指的是没有项目,它应该是 Int 和 Price 是一个浮点数 属性。 TotalArticlePrice
是计算出来的 属性.
public class Article
{
public float Price { get; set; }
public int Quantity { get; set; }
public float TotalArticlePrice
{
get
{
return Price * Quantity;
}
}
}
正如您提到的,一个 BillLine 有一篇文章,因此模型应如下所示,其中 Article
属性 和 FinalBillLinePrice
是在扣除折扣值
public class BillLine
{
public int DiscountValue { get; set; }
public Article Article { get; set; }
public float FinalBillLinePrice
{
get
{
return Article.TotalArticlePrice - DiscountValue;
}
}
}
最后,
正如您提到的,一个 Bill
可能有多个 BillLine
,因此模型应如下所示,其中 Total
属性 存储总账单价格。
public class Bill
{
public float Total
{
get
{
return BillLineList.Sum(x => x.FinalBillLinePrice);
}
}
public List<BillLine> BillLineList { get; set; }
}
以防万一,我有什么误解,请在评论中简要说明。