对不起,如果我的问题太基本了,我只是新的MVC没有经验的JavaScript。MVC计算字段和字段格式
1 - 我想添加计算领域在我看来没有成功 我有下面的类
public class TaxCalculation
{
[Required]
[DisplayName("Init")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
public double InitVlaue { get; set; }
[Required]
[DisplayName("Reg")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
public double RegValue { get; set; }
[DisplayName("Enga")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
public double InitEnga
{
get { return CommonComputation.CalcuEnga(InitVlaue, RegValue); }
}
}
CommonComputation.CalcuEnga功能:
public static double CalcuEnga(double InitVlaue, double RegValue)
{
double V_Et = (0.02 * 0.225 - 1) * Math.Log(0.225/(100 - 0.225));
double V_En = (0.02 * 0.711 - 1) * Math.Log(0.711/(100 - 0.711));
double mk = Math.Round(RegValue/InitVlaue * 100, 3);
double V_Ep = (0.02 * InitVlaue - 1) * Math.Log(InitVlaue/(100 - InitVlaue));
double F = Math.Round((InitVlaue - 0.225)/(0.711 - 0.225), 5);
double S = Math.Round(((V_Ep) + (F - 1) * V_Et - F * V_En), 5);
double ceq = Math.Round(F + 1.53 * S, 5);
return Math.Round(mk * ceq, 3);
}
我的看法是
<div class="editor-label">
@Html.LabelFor(model => model.InitVlaue)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.InitVlaue,new {style="width:50px;"})
@Html.ValidationMessageFor(model => model.InitVlaue)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RegValue)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.RegValue,new {style="width:50px;"})
@Html.ValidationMessageFor(model => model.RegValue)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.InitEnga)
</div>
<div class="editor-field">
@Html.TextBoxFor(x=> x.InitEnga,new {name="initEngaTextBox",style="width:50px;",@readonly="readonly"})
@Html.ValidationMessageFor(model => model.InitEnga)
</div>
Iwant当用户键入任何前2个字段(InitVlaue
或RegValue
) 提交的InitEnga
得到更新 我试图直接添加该字段或调用执行计算没有成功的函数。 我搜索并找到1000年的答案使用的东西像阿贾克斯,淘汰赛...等 我没有任何经验与JavaScript的经验。 有没有简单的方法来解决这个问题?
2-在所有的文本框,虽然我指定的格式[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
,我仍然看到值0,任何想法是什么错?
非常感谢
首先,当你使用EditorFor()和DisplayFor()时,你的'DisplayFormatAttribute'只有在使用' TextBoxFor()')。其次,如果您想对客户端事件做出反应,那么您需要使用javascript/jquery –
使用纯服务器端MVC无法实现此目的。如果您想在动态更新前两个字段时重新计算第三个值,则需要使用javascript。如果执行计算的函数是服务器端函数,则需要将这些值发送到可以通过AJAX实现的服务器。所以我建议你开始学习JavaScript,如果你不熟悉它。 –
C#中的MVC代码在服务器上执行,而不是在客户端中执行。因此,当您更改其他2的值时,第三个不会更新,因为没有进行服务器调用。 您需要的是简单的Javascript,它将在客户端执行计算或将更改的值发送回服务器,并获取更新的值并在UI中显示更新的值。 因为这对我来说看起来像简单的计算,我会建议使用javascript在客户端上执行计算器。 – Daredevil