2016-03-06 107 views
0

對不起,如果我的問題太基本了,我只是新的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個字段(InitVlaueRegValue) 提交的InitEnga得到更新 我試圖直接添加該字段或調用執行計算沒有成功的函數。 我搜索並找到1000年的答案使用的東西像阿賈克斯,淘汰賽...等 我沒有任何經驗與JavaScript的經驗。 有沒有簡單的方法來解決這個問題?

2-在所有的文本框,雖然我指定的格式[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")],我仍然看到值0,任何想法是什麼錯?

非常感謝

+1

首先,當你使用EditorFor()和DisplayFor()時,你的'DisplayFormatAttribute'只有在使用' TextBoxFor()')。其次,如果您想對客戶端事件做出反應,那麼您需要使用javascript/jquery –

+1

使用純服務器端MVC無法實現此目的。如果您想在動態更新前兩個字段時重新計算第三個值,則需要使用javascript。如果執行計算的函數是服務器端函數,則需要將這些值發送到可以通過AJAX實現的服務器。所以我建議你開始學習JavaScript,如果你不熟悉它。 –

+1

C#中的MVC代碼在服務器上執行,而不是在客戶端中執行。因此,當您更改其他2的值時,第三個不會更新,因爲沒有進行服務器調用。 您需要的是簡單的Javascript,它將在客戶端執行計算或將更改的值發送回服務器,並獲取更新的值並在UI中顯示更新的值。 因爲這對我來說看起來像簡單的計算,我會建議使用javascript在客戶端上執行計算器。 – Daredevil

回答

2

爲了給客戶端的響應事件,你需要使用JavaScript/jQuery的。在你的情況下,通過處理文本框的.change()事件。通過給你2名可編輯的文本框類名

@Html.TextBoxFor(m => m.InitVlaue, "{0:#.#}", new { @class="calc" }) 
@Html.TextBoxFor(m => m.RegValue, "{0:#.#}", new { @class="calc" }) 

注意的第二個參數是格式字符串開始(DisplayFormatAttribute僅由EditorFor()DisplayFor()方法推崇這樣他們就可以,除非你在其他地方使用這些方法來刪除),您現在可以使用CSS來設置寬度的樣式 - .calc { width: 50px; }

您可以使用javascript在客戶端上進行計算,但由於它很複雜,這意味着需要在服務器和客戶端上進行維護,所以我建議您使用ajax調用執行計算並返回值的服務器方法。

添加jquery-{version}.js到您的視圖(或佈局),並添加下面的腳本

<script> 
    var url = '@Url.Action("Calculate", "yourControllerName")'; 
    $('.calc').change(function() { 
     // get the values of the textboxes 
     var init = $('#InitVlaue').val(); 
     var reg = $('#RegValue').val(); 
     // Check they are valid 
     if (init == '' || reg == '' || isNaN(init) || isNaN(reg)) { 
      return; 
     } 
     $.post(url, { initVlaue: init, regValue: reg }, function(response) { 
      $('#InitEnga').val(response); 
     }); 
    }); 
</script> 

,並添加以下控制器方法

[HttpPost] 
public JsonResult Calculate(double initVlaue, double regValue) 
{ 
    var result = CommonComputation.CalcuEnga(initVlaue, regValue); 
    return Json(result.ToString("#.#")); 
} 

要了解更多關於jQuery,請參閱documentation

+0

非常感謝, 如果我更改爲** Html.EditorFor **第二個參數給出錯誤**無法解析模板{0:#。#} ** – Maro

+0

您不能像這樣使用EditorFor()。只要使用我給你的代碼,但是如果你想使用'EditorFor()',那麼它的只是'Html.EditorFor(m => m.RegValue)'(它將使用'DataFormatString'格式化值。但是,因爲你不使用MVC-5.1 +,那麼你將無法添加HTML屬性,所以它不適合你的情況。 –

+0

感謝你的回答,我遵循你的步驟,並有以下問題:1-字段不是提交時,但我看到號碼222當表單被加載,數字被添加到2 fileds之間的某處,2 - 關於IsNaN的錯誤,但解決使用isNaN,** $('#InitVlaue')**是這個文本框ID或名字?3-我在json動作中放了一個休息,但是沒有到達 – Maro