C# 如何从mvc视图中提取主要价格值?

C# 如何从mvc视图中提取主要价格值?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我在这里做了很多假设,因为你的问题需要更多的细节,但看起来cPrice是你的基类,属性为MainPrice 因此,如果您的视图声明是 this.ProductOptions[0] {MAFIA webEPOS.Models.cProductSize} base {webEPOS.Models.cPrice}: {MAFIA webEPOS.Models.cProductSize} IsDeleted: false OptionID: 12 ParentName:

我在这里做了很多假设,因为你的问题需要更多的细节,但看起来cPrice是你的基类,属性为MainPrice

因此,如果您的视图声明是

this.ProductOptions[0]
{MAFIA webEPOS.Models.cProductSize}
    base {webEPOS.Models.cPrice}: {MAFIA webEPOS.Models.cProductSize}
    IsDeleted: false
    OptionID: 12
    ParentName: "MAFIA"
    Position: 0
    ProductID: 7
    SizeDescription: "7''"
    SizeID: 1
    SizeInfo: {webEPOS.Models.cProductSize}
我猜Items集合是松散类型的,因为您编写上述代码的方式。您不想在视图中随意使用这种代码;通过在cProductOption上编写一个扩展方法,在服务器端安全地执行此操作,该方法将优雅地抓取它:

return View(this.ProductOptions);
然后,因为您已经按照您的方式对继承进行了排序,所以您可以简单地迭代模型列表:

public static decimal MainPrice (this cProductOption cproductOption) {

    decimal returnValue = 0m;

    try {
        // returnValue = try and get it from .Items[0], casting to the type you need and/or seeing if the necessary relationships exist.
    }
    catch(Exception exception){
        // log the possible exception
    }

    return returnValue;
}
就像我说的,我正在对其中的一些进行有根据的猜测,但希望它能给你一些提示。祝你的应用程序好运

return View(this.ProductOptions);
public static decimal MainPrice (this cProductOption cproductOption) {

    decimal returnValue = 0m;

    try {
        // returnValue = try and get it from .Items[0], casting to the type you need and/or seeing if the necessary relationships exist.
    }
    catch(Exception exception){
        // log the possible exception
    }

    return returnValue;
}
@foreach (cProductOption cproductOption in Model) {
    @cProductOption.MainPrice()
}