C# 在提交MVC4时在视图内创建视图

C# 在提交MVC4时在视图内创建视图,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-partialview,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Partialview,我有一个带有表单的视图,提交表单时,我希望在第一个视图中显示另一个视图。我尝试过partialview(不确定这是否是为了我的目的?),但我无法让它工作,因为模型为null(它的强类型partialview)。但我只想在submitbutton上创建它,然后它就可以了 因此,请在提交表单时帮助我在中创建视图/局部视图。现在我在部分视图中的foreach循环中得到了“objectreference not set to a instance of a Object.”,但在提交表单之前,不应该创建

我有一个带有表单的视图,提交表单时,我希望在第一个视图中显示另一个视图。我尝试过partialview(不确定这是否是为了我的目的?),但我无法让它工作,因为模型为null(它的强类型partialview)。但我只想在submitbutton上创建它,然后它就可以了

因此,请在提交表单时帮助我在中创建视图/局部视图。现在我在部分视图中的foreach循环中得到了“objectreference not set to a instance of a Object.”,但在提交表单之前,不应该创建它

我的看法是:

<div class="calcInput">
                    @using (Html.BeginForm("ShowDetail", "Calculation"))
                    {
                       <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>
                    }
                    </div>
                    <table cellspacing="0" width="80%">
                    <div class="calcGraph">
                    </div>
                    <div class="calcDetail" id="anCalcDetail">
                    @Html.Partial("ShowDetail") //This is where I want my view/partialview to show up
                    </div>

@使用(Html.BeginForm(“ShowDetail”、“Calculation”))
{
选择要计算的值:
付款(PMT)
利息(I)
未来价值(FV)
现值(PV)
@Html.Label(“现值(PV)”)
@Html.TextBox(“PresentValue”)
@Html.Label(“未来值”)
@Html.TextBox(“未来价值”)
@Html.标签(“利率”)
@Html.TextBox(“利率”)
提前
拖欠 开始日期:
@Html.Label(“付款频率”) 月刊 每季的 每年

@Html.标签(“付款期编号”) @Html.TextBox(“付款期”) @Html.Label(“日期时间约定”) 360/360 365/365

} @Html.Partial(“showtail”)//这是我希望显示我的视图/partialview的地方
我的观点:

@model IEnumerable<CalcFactory.Models.Calculation>

<table cellspacing="0" width="80%">
<thead>
    <tr>
        <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>
<tr>
    <td align="center">
    </td>
    <td align="center">
    </td>
    <td align="center">
    </td>
    <td align="center">
    </td>
    <td align="center">
    </td>
    <td align="center" id="startValue">
    </td>
</tr>
@foreach (var item in Model) { //this is where i get null exception, model is null. But it supposed to be created on submit button...
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InvoiceAmount)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InterestRate)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InterestAmount)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Amortization)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PresentValue)
    </td>
</tr>
}
@model IEnumerable
日期
发票金额
利率
利息金额
摊销
资本余额
@foreach(模型中的var项){//这是我得到null异常的地方,模型为null。但它应该在提交按钮上创建。。。
@DisplayFor(modelItem=>item.Date)
@DisplayFor(modelItem=>item.InvoiceAmount)
@DisplayFor(modelItem=>item.InterestRate)
@DisplayFor(modeleItem=>item.InterestAmount)
@DisplayFor(modelItem=>item
@DisplayFor(modelItem=>item.PresentValue)
}

我的控制器:

public class CalculationController : Controller
{
    public PartialViewResult ShowDetail(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;
        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 * Convert.ToDouble(form["InterestRate"]) / 360 * 30);
            calc.Amortization = (calc.InvoiceAmount - (calcBefore.PresentValue * calc.InterestRate / 360 * 30));
            calc.PresentValue = calcBefore.PresentValue - calc.Amortization;
            cList.Add(calc);
        }
        return PartialView("ShowDetail", cList);
    }
}
公共类计算控制器:控制器
{
公共PartialViewResult ShowDetail(表格收集表格)
{
List cList=新列表();
计算计算=新计算();
计算日期=转换日期时间(格式[“开始日期]);
计算发票金额=2000;
calc.InterestRate=Convert.ToDouble(形式[“InterestRate”]);
计算利息金额=(Convert.ToDouble(表格[“PresentValue”])*Convert.ToDouble(表格[“InterestRate”])/360*30);
计算摊销=(2000年-(折算至双倍表[“现值”])*折算至双倍表[“利率”])/360*30);
计算现值=换算成双倍(表[“现值])-计算摊销;
客户添加(计算);
对于(inti=0;i
不必直接在主视图中呈现部分视图,您可以通过ajax调用获取部分视图,只需将html转储到主视图的空div中即可。你需要将所有必需的参数传递给
Action
method(在你的情况下,我相信这将是付款期)。你能用代码告诉我这是什么样子吗?我现在还没有访问VS的权限,但快速的google可能会帮你找到打电话的方法。这里是我找到的一个链接。还有一个。另一个我会看看,但这是否意味着我不使用我的控制器和ShowDetail操作?这也可以在提交时实现。访问模型属性时只需小心。如果传递给局部视图的模型为空,则会引发异常。为此,您可以始终在主视图中检查当前模型是否为Null。如果不为空,则调用
PartialView(“showtail”,cList)