当改变MVC值时,从模型更新表

本文关键字:模型 更新 改变 MVC 值时 | 更新日期: 2023-09-27 17:49:31

我的表是这样的:

@{var i = true;}
@{var rowID = 1;}
@foreach (var item in Model) {
    if (i)
    {
        <tr id="@rowID">
            <td align="center">
                @rowID
            </td>
            <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">
                @Html.DisplayFor(modelItem => item.StartValue)
            </td>
        </tr>
        i = false;
        rowID++;
    }
    <tr id="@rowID">
        <td align="center">
            @rowID
        </td>
        <td align="center">
            <div contenteditable>
                @Html.DisplayFor(modelItem => item.Date)
            </div>
        </td>
        <td align="center">
            <div contenteditable>
            @Html.DisplayFor(modelItem => item.InvoiceAmount)
            </div>
        </td>
        <td align="center">
            <div contenteditable>
            @Html.DisplayFor(modelItem => item.InterestRate)
            </div>
        </td>
        <td align="center">
            <div contenteditable>
            @Html.DisplayFor(modelItem => item.InterestAmount)
            </div>
        </td>
        <td align="center">
            <div contenteditable>
            @Html.DisplayFor(modelItem => item.Amortization)
            </div>
        </td>
        <td align="center">
            <div contenteditable>
            @Html.DisplayFor(modelItem => item.PresentValue)
            </div>
        </td>
    </tr>
    rowID++;
}

我已经使每个单元格在表格中使用他们的div标签编辑。模型相互依赖,这意味着第二行上的对象依赖于第一行对象上的属性值,以此类推。我不想从我改变的地方更新表(如果我改变第5行,我希望下面的行根据我在第5行上的改变而改变)。我不知道如何使表像这样更新,真的很感谢对此的一些帮助。

CalclationController中创建模型对象的方法:

public ActionResult 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;
        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 PartialView("ShowDetail", cList);
    }

那么,有没有办法从选定的行和下面的更新我的表?

当改变MVC值时,从模型更新表

你的意思是改变行数据自动取决于行你只是编辑它的数据?我认为你可以使用javascript来flash下面的行当你完成编辑,并使用ajax来更新新的数据