2014-03-14 49 views
0

下面的代碼給了我「對象引用未設置爲對象的實例」。我知道這是因爲模型沒有創建,但我不明白爲什麼?爲什麼AnnuityDetail.cshtml中的foreach循環沒有通過cList創建並從Controller發送?將對象列表從控制器返回到視圖

控制器動作:

public ActionResult AnnuityDetail(FormCollection form) 
    { 
     List<Calculation> cList = new List<Calculation>(); 
     Calculation calc = new Calculation(); 
     calc.Date = Convert.ToDateTime(form["startdate"]); 
     calc.InvoiceAmount = 2000; 
     calc.InterestRate = Convert.ToDouble(form["InterestRate"]); 
     calc.InterestAmount = (Convert.ToDouble(form["PresentValue"]) * Convert.ToDouble(form["InterestRate"])/360 * 30); 
     calc.Amortization = (2000 - (Convert.ToDouble(form["PresentValue"]) * Convert.ToDouble(form["InterestRate"])/360 * 30)); 
     calc.PresentValue = Convert.ToDouble(form["PresentValue"]) - calc.Amortization; 
     calc.StartValue = Convert.ToDouble(form["PresentValue"]); 
     cList.Add(calc); 
     for (int i = 0; i < Convert.ToInt32(form["PaymentPeriods"]); i++) 
     { 
      Calculation calcBefore = cList.Last(); 
      calc = new Calculation(); 
      calc.Date = calcBefore.Date.AddMonths(1); 
      calc.InvoiceAmount = 2000; 
      calc.InterestRate = Convert.ToDouble(form["InterestRate"]); 
      calc.InterestAmount = (calcBefore.PresentValue * calc.InterestRate/360 * 30); 
      calc.Amortization = (calc.InvoiceAmount - (calcBefore.PresentValue * calc.InterestRate/360 * 30)); 
      calc.PresentValue = calcBefore.PresentValue - calc.Amortization; 
      cList.Add(calc); 
     } 
     return View(cList); 
    } 

視圖1(以一種形式)

@using (Html.BeginForm("AnnuityDetail", "Calculation")) //Runs AnnuityDetail 
{ 
    <div class="calculateBox"> 
          <label for="calcOption">Choose value to calculate:</label> 
          <select form="anFormCalcInput" id="calcOption" title="Choose what value you want to calculate"> 
           <option value="anPMT">Payment (PMT)</option> 
           <option value="anI">Interest (I)</option> 
           <option value="anFV">Future value (FV)</option> 
           <option value="anPV">Present value (PV)</option> 
          </select> 
         </div> 
         <div class="calculateBox" background-color="#777777"> 
          @Html.Label("Present value (PV)") 
          @Html.TextBox("PresentValue") 
          @Html.Label("Future value") 
          @Html.TextBox("FutureValue") 
          @Html.Label("Interest rate") 
          @Html.TextBox("InterestRate") <br /> 
          <input type="radio" name="advanceOrArrears" id="inAdvance" value="inAdvance" /> In advance<br /> 
          <input type="radio" name="advanceOrArrears" id="inArrears" value="inArrears" /> In arrears 
         </div> 
         <div class="calculateBox"> 
          <label for="startDate">Start date:</label> 
          <input type="date" id="anStartDate" name="startdate" title="Choose start date for your calculation" /><br /> 
          @Html.Label("Payment frequency") 
          <select form="anFormCalcInput" id="anPmtFreq" title="Choose the payment frequency, for example: Every month"> 
           <option value="Monthly">Monthly</option> 
           <option value="Quarterly">Quarterly</option> 
           <option value="Yearly">Yearly</option> 
          </select><br /><br /> 
          @Html.Label("No of payment periods") 
          @Html.TextBox("PaymentPeriods") 
          @Html.Label("Date time convention") 
          <select form="anFormCalcInput" id="anDTC" title="Choose your Date time convention"> 
           <option value="360360">360/360</option> 
           <option value="365365">365/365</option> 
          </select><br /><br /> 
          <input type="submit" id="anCalcBtn" class="calcBtn" name="anSubmitBtn" value="Calculate" title="Calculate your calculation" /> 
         </div> 
} 

視圖2(AnnuityDetail):

@model IEnumerable<CalcFactory.Models.Calculation> 
<table cellspacing="0" width="80%" id"detailTable"> 
<thead> 
    <tr> 
     <th> 
      Row 
     </th> 
     <th> 
      Date 
     </th> 
     <th> 
      Invoice amount 
     </th> 
     <th> 
      Interest rate 
     </th> 
     <th> 
      Interest amount 
     </th> 
     <th> 
      Amortization 
     </th> 
     <th> 
      Capital balance 
     </th> 
     <th></th> 
    </tr> 
</thead> 
@{var rowID = 1;} 
@{var cellID = 1;} 
@foreach (var item in Model) { //In this loop I get my error message, it can't find cList objects 
    <tr id="@rowID"> 
     <td align="center"> 
      @rowID 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.Date) 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.InvoiceAmount) 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.InterestRate) 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.InterestAmount) 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.Amortization) 
     </td> 
     <td align="center" id="[email protected]"> 
      @Html.DisplayFor(modelItem => item.PresentValue) 
     </td> 
    </tr> 
    rowID++; 
    cellID++; 
} 

回答

2

您的表單中不包含任何「付款」字段期間「,所以該值不被初始化。 for循環立即達到其結束條件。

如果PaymentPeriods應該來自表格但不可編輯嘗試添加一個隱藏的字段與您需要的值,但它應該在客戶端呢?


應該不是你的@Html.DisplayFor(modelItem => item.Date)@Html.DisplayFor(item => item.Date)呢? modelItem是不是用在任何地方?


能否請你嘗試改變var itemCalculation item,看看屬性還沒有解決?

@foreach (Calculation item in Model) { //In this loop I get my error message, it can't find cList objects 
    <tr id="@rowID"> 
     <td align="center"> 
      @rowID 
     </td> 
     <td align="center" id="[email protected]"> 
     @Html.DisplayFor(item => item.Date) 
    </td> 
+0

對不起,我會更新表單代碼。 PaymentPeriods存在,所以循環應該運行 – MrProgram

+0

但實際上並不意味着你只是將一個空列表引用傳遞給視圖。循環不會迭代。然而,操作是說,他得到「對象參考」錯誤@該線... – deostroll

+0

@deostroll你說得對,我在注意問題的第一部分問爲什麼for循環沒有運行。查看更新後的代碼 – samy

相關問題