0
아래의 코드는 "객체 참조가 객체의 인스턴스로 설정되지 않았습니다"를 제공합니다. 모델이 만들어지지 않았기 때문에 그것이 나왔다는 것을 압니다. 그러나 나는 그 이유를 이해할 수 없습니까? AnnuityDetail.cshtml의 foreach 루프가 컨트롤러에서 만들고 보내는 cList를 통해 루프를 실행하지 않는 이유는 무엇입니까?컨트롤러에서보기로 객체 목록 반환
컨트롤러 조치 :
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++;
}
이
죄송합니다. 양식 코드가 업데이트됩니다. PaymentPeriods가 있으므로 루프가 – MrProgram
을 실행해야합니다. 실제로는 빈 목록 참조를보기로 전달한다는 의미는 아닙니다. 루프는 반복하지 않습니다. 그러나 Op는 그가 "Object Reference"오류 @ 그 라인을 얻는다 고 말하고 있습니다 ... – deostroll
@deostroll 맞습니다. for 루프가 실행되지 않는 이유를 묻는 질문의 첫 부분에 고정되어 있습니다. 업데이트 된 코드 – samy