我怎么可以归纳传递到模型中的两个不同的选择? [英] How can I SUM two different selections passed to model?

查看:184
本文介绍了我怎么可以归纳传递到模型中的两个不同的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进一步采取了事先问题一步(见这个问题),我试图找出用户如何两个(或更多)的选择使得有,例如,一个单选按钮列表。用户进行的选择是依赖于使用包含静态币值实体的if / else,如果语句。

这些都是实体价格:

  [数据类型(DataType.Currency)
[DisplayFormat(DataFormatString ={0:C})]
公共小数priceProcessingStandard = 0;[数据类型(DataType.Currency)
[DisplayFormat(DataFormatString ={0:C})]
公共小数priceProcessingExpedited = 250;[数据类型(DataType.Currency)
[DisplayFormat(DataFormatString ={0:C})]
公共小数priceSubmissionOnline = 0;[数据类型(DataType.Currency)
[DisplayFormat(DataFormatString ={0:C})]
公共小数priceSubmissionManual = 200;

所以,如果我有两套中的if / else,如果语句,如:

  @if(Model.ProcessingRadioButtons == Processing.Standard)
{
    @ Html.DisplayFor(M = GT; m.priceProcessingStandard)
}
否则,如果(Model.ProcessingRadioButtons == Processing.Expedited)
{
    @ Html.DisplayFor(M = GT; m.priceProcessingExpedited)
}
...
@if(Model.SubmissionRadioButtons == Submission.Online)
{
    @ Html.DisplayFor(M = GT; m.priceSubmissionOnline)
}
否则,如果(Model.SubmissionRadioButtons == Submission.Manual)
{
    @ Html.DisplayFor(M = GT; m.priceSubmissionManual)
}

和用户进行选择在两个单独的单选按钮,列表中的相应于 Processing.Expedited Submission.Manual 中,code将分别显示 $ 250.00 $ 200.00

我不能,但是,弄清楚如何这两个显示 $ 450.00 。请记住,我不知道前手的选择,这样算下来 priceProcessingExpedited + priceSubmissionManual 在函数中,然后调用它显然是不行的。另外,我做一下这些10-15,但我只用两个简单的那些为我所要完成的一个例子(这样的事实,另外两个选择是 $ 0.00包装,因为有不同的价格,我离开了其他选择)并不意味着什么。

任何指导?

更新:
根据答案的建议,我这样做:

  Model.calculated =
    Model.priceSolution +
    ((Model.ProcessingRadioButtons == Processing.Standard)?
    Model.priceProcessingStandard:
    (Model.ProcessingRadioButtons == Processing.Expedited)?
    Model.priceProcessingExpedited:
    Model.priceProcessingUrgent);

一些注意事项:


  1. priceSolution 是我作为一个基地使用静态值(它的基值加上用户选择)。

  2. 我使用计算在视图模型和搞定;设置; ING它

  3. 我离开了 Namespace.ViewModels.MyData 处理。为简洁。

  4. 我离开了提交为简单起见,因为它只是一个 + 那么同样的逻辑在处理


解决方案

您只知道前手的选择考虑到你的 @if(Model.SubmissionRadioButtions == Submission.Online)反对目前由模型所保持的值进行测试 - 即使这仅仅是一个POST之后

因此​​,你应该建立在你的视图模型的属性也进行这些测试,总结相应的字段。

如果您不想显示该属性的 的自检之前,请属性返回一个空类型,敷用 @if视图(MySum.HasValue){@ Html.DisplayFor(M = GT; m.MySum)}

I am taking a prior question one step further (see this question), I am trying to figure out how to sum two (or more) selections the user makes with, for example, a radio button list. The selection the user makes is tied to an entity that contains a static currency value using if/else if statements.

These are the entities for price:

[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceProcessingStandard = 0;

[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceProcessingExpedited = 250;

[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceSubmissionOnline = 0;

[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceSubmissionManual = 200;

So, if I have two sets of if/else if statements such as:

@if (Model.ProcessingRadioButtons == Processing.Standard)
{
    @Html.DisplayFor(m => m.priceProcessingStandard)
}
else if (Model.ProcessingRadioButtons == Processing.Expedited)
{
    @Html.DisplayFor(m => m.priceProcessingExpedited)
}
...
@if (Model.SubmissionRadioButtons == Submission.Online)
{
    @Html.DisplayFor(m => m.priceSubmissionOnline)
}
else if (Model.SubmissionRadioButtons == Submission.Manual)
{
    @Html.DisplayFor(m => m.priceSubmissionManual)
}

and the user makes selections in the two separate radio button lists corresponding to Processing.Expedited and Submission.Manual, the code will respectively display $250.00 and $200.00.

I cannot, however, figure out how to sum those two to display $450.00. Bear in mind, I do not know the selections before hand, so doing priceProcessingExpedited + priceSubmissionManual in a function and then calling it will obviously not work. Also, I am doing about 10-15 of these but I only used two simple ones as an example of what I am trying to accomplish (so the fact that the other two choices are $0.00 doesn't mean anything because there are varying prices for other choices that I left out).

Any guidance?

UPDATE: Based on suggestion in answer, I am doing this:

Model.calculated =
    Model.priceSolution +
    ((Model.ProcessingRadioButtons == Processing.Standard) ?
    Model.priceProcessingStandard :
    (Model.ProcessingRadioButtons == Processing.Expedited) ?
    Model.priceProcessingExpedited :
    Model.priceProcessingUrgent);

Some notes:

  1. priceSolution is a static value that I use as a base (it's the base value plus the user selections).
  2. I am using calculated in the ViewModel and get; set;'ing it.
  3. I left out the Namespace.ViewModels.MyData before Processing. for brevity.
  4. I left out Submission for brevity as it's just a + then the same logic as in Processing.

解决方案

You do know the selections before hand considering your @if (Model.SubmissionRadioButtions == Submission.Online) is a test against values currently held by the model - even if this is only after a POST.

As such, you should create a property in your view model that also performs these tests and sums the appropriate fields.

If you don't want this property displayed before the POST, make the property return a nullable type and wrap the view with @if(MySum.HasValue) { @Html.DisplayFor(m=>m.MySum) }

这篇关于我怎么可以归纳传递到模型中的两个不同的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆