2

我正在尝试计算价格,例如:

@{
    Model.Prices.total =
        ((Input == choice) ? a : b)
    +
        ((AnotherInput == anotherchoice) ? c : d)
    ;
}

这在.cshtml视图文件中运行良好,但不用说实际代码要长很多(更不用说我想应用 3 种不同的计算)。

所以我开始考虑在调用中创建一个@helper文件,这样我就可以这样调用它:App_CodeCalculatePricing.cshtml

@Pricing.Calculate()

但这会引发“无法对空引用执行运行时绑定”错误。

我得到了错误,我明白这是一个限制。谁能建议我如何才能做到这一点?我想到了一个类文件,但不知道如何将它转换成一个(如果这是一个更好的选择)。非常感谢代码示例。

更新: 我暂时将此计算放在视图中,因为我无法将其转换为我研究过的其他选项:

  1. 文件中的一个@helper函数App_Code- 但这是我遇到的“空引用”错误。
  2. 一个.cs类文件(或者可能只是将它添加到我的 ViewModel - 但我无法正确编码。
  3. 将它放在控制器中的建议 - 但代码很长。
  4. 创建一个自定义的 Html Helper 类来尝试将其称为 `@Html.Calculate([parameter?], [parameter?]) - 但对我来说,这比 #2 复杂一些。- 适当注明的评论。

再次注意,我提供的示例是一个简单的、简单的示例。我有 30 多个条件可以用多个选项来探索每个条件(例如上面的a's 和b'),其中一些变得更加复杂:

() ? a : () ? b : c + () ? d : () ? e : () ? f : g + ....

如果我使用 C# 代码(在它自己的.cs文件、自定义 Html Helper 或 ViewModel 中),所有的声明都会出错。

请注意,这不是我遇到问题的实际计算。上面的那个片段可以用来执行我的计算。I am just struggling to get it to an appropriate option I listed above.

UPDATE 2

I am having a lot of trouble converting this to it's own class (I am not a programmer - sorry for not understanding). This is part of my function in the view (just a snippet, hopefully I can figure out the rest if you provide a code sample):

@{
    Model.Price.calculated
        =
        //below is the "base" price, all else would be "add-ons"
        Model.Price.priceOne //elsewhere would be priceTwo, etc.
        +
            ((Model.MyModelOne.MyRadioButtonOne == 
                MyModelOne.RB1Enum.RB1ChoiceOne)
            ?
            Model.Price.AddOnOne
            :
            (Model.MyModelOne.MyRadioButtonOne == 
                MyModelOne.RB1Enum.RB1ChoiceTwo)
            ?
            Model.Price.AddOnTwo
            :
            Model.Price.AddOnThree)
    +
        ((Model.MyModelTwo.MyRadioButtonTwo
            == MyModelTwo.RB2Enum.RB2ChoiceOne)
        ?
        Model.Price.AddOnFour
        :
        (Model.MyModelTwo.MyRadioButtonTwo
            == MyModelTwo.RB2Enum.RB2ChoiceTwo)
        ?
        Model.Price.AddOnFive
        :
        Model.Price.AddOnSix)
    ;
}

Don't bust my balls for using enums. :)

Again, although the calculation is not the appropriate thing to do in a view, the above works - I get the calculated result I am expecting based on user choices.

My Price.cs model (again, just something basic so you get the idea):

public class Price
{
    //Leaving out [DataType] and [DisplayFormat] DataAnnotations
    // leaving out priceTwo, etc.
    // numbers are basic for simplicity
    public decimal calculated { get; set; }
    public decimal priceOne { get { return 100; } } 
    public decimal AddOnTwo { get { return 10; } } 
    public decimal AddOnThree { get { return 20; } }
    public decimal AddOnFour { get { return 30; } }
    public decimal AddOnFive { get { return 40; } }
    public decimal AddOnSix { get { return 50; } }
    // Others go here
}
4

1 回答 1

1

Html helper functions are just ways to reuse duplicated pieces of your display. What you're doing now is calculations based on the state of your model. In MVC, this data processing would be considered part of the model.

Here's the basic steps I'd take.

  1. Fetch/create your business model(s), which contains all the information necessary to do this calculation.
  2. Perform the calculation on the business model. If your calculation is based on one object using only it's internal properties, you can include the calculation code within your business model itself. Otherwise I'd create a separate calculations class that computes it.
  3. Create your View model based on the calculation result and your business model. The view model should only contain the state that will be displayed/edited within the view.

For example:

public ActionResult YourAction(int userChoice)
{
    MyModel businessModel = createOrFetchModel(); //May be more than one object
    Calculations calcs = new Calculations(businessModel);
    var total = calcs.GetTotal(userChoice);
    MyViewModel viewModel = makeViewModel; //usually I'd use something like AutoMapper here
    return View(viewModel);
}

Edit : Sample code based on your update. The final design you must decide based on the actual calculation.

 public class Calculations
 {
     MyModelOne modelOne; //Do you really need seperate models for radio buttons?
     MyModelTwo modelTwo;
     Price Price;

     //...

     public decimal GetTotal()
     {
         decimal total = price.priceOne;
         total += price.FirstAddOn(modelOne);
         total += price.SecondAddOn(modelTwo);
         return total;
     }
 } 

 public class Price
 {
     //...
     public decimal FirstAddOn(MyModelOne modelOne)
     {
       if(modelOne.MyRadioButtonOne == MyModelOne.RB1Enum.RB1ChoiceOne)
          return this.AddOnOne;
       else if(modelOne.MyRadioButtonOne == MyModelOne.RB1Enum.RB1ChoiceTwo)
          return this.AddOnTwo;
       else
         return this.AddOnThree;
     }
 }
于 2012-05-18T14:26:56.040 回答