如何在回发期间保留模型值?

时间:2015-05-08 11:23:56

标签: asp.net-mvc asp.net-mvc-5

我有一个模特:

public class EmployeeDetails
{
    public int staffcode { get; set; }
    public string name { get; set; }
    public string designation { get; set; }
    public string department { get; set; }
}

单击“搜索”按钮,将从数据库中获取员工详细信息,并初始化模型属性。这些值也显示在视图上。

            dtEmpDetails = lib.RunSP("GetEmpDetails", lstEmpDetails);

            leaveBalanceViewModel.EmployeeDetail.staffcode = Convert.ToInt32(staffCode);
            leaveBalanceViewModel.EmployeeDetail.name = dtEmpDetails.Rows[0][0].ToString();
            leaveBalanceViewModel.EmployeeDetail.designation = dtEmpDetails.Rows[0][1].ToString();

还有另一个按钮保存。当我单击“保存”时,View中的文本框值会丢失,因为再次发生提交。

如何存储模型属性值并在回发期间保持可用状态?

**已编辑** 查看代码如下:

<div>

    @using (Html.BeginForm("Search", "AFLeaveBalance",FormMethod.Get))
    {
        <table style="border:none">
            <tr>
                <td style="border:none">Enter staff code</td>
                <td style="border:none">@Html.TextBox("staffcode", "", new { width = "100" })</td>
                <td style="border:none"><input type="submit" value="Get Details" /></td>
            </tr>
        </table>
    }
      <br /><hr />  
     <table id="tblEmpDetails">
         <tr>
             <td style="background-color:#d2eef7; width:200px;text-align:left">Employee Name</td>
             <td>@Html.TextBoxFor(m => m.EmployeeDetail.name, new { @readonly = "ReadOnly", @class = "ReadOnly", style = "width:300px" })</td>
             @Html.HiddenFor(m => m.EmployeeDetail.name)
         </tr>
        <tr>
            <td style="background-color:#d2eef7;width:200px;text-align:left">Designation</td>
             <td >
                @Html.TextBoxFor(m => m.EmployeeDetail.designation, new { @readonly = "ReadOnly", @class = "ReadOnly", style="width:300px" })
             </td>
        </tr>
         <tr>
             <td style="background-color:#d2eef7;width:200px;text-align:left">Directorate</td>
             <td>@Html.TextBoxFor(m => m.EmployeeDetail.directorate, new { @readonly = "ReadOnly", @class = "ReadOnly", style = "width:300px" })</td>
        </tr>
     </table>
        <br /><hr />

@using (Html.BeginForm("Save", "AFLeaveBalance", FormMethod.Post))
{
    <table id="tblCurrentYear">
        <tr style="background-color:#d2eef7;color:Black;font-weight:200">
            <td colspan="4">
                @Html.Label("Year: " + DateTime.Now.Year.ToString())
            </td>
        </tr>
        <tr style="background-color:#d2eef7;color:Black;">
            <td>Leave Type</td>
            <td>Leave Taken</td>
            <td>Leave Balance</td>
            <td>Leave Total</td>
        </tr>
        @*@for (int i = 0; i < Model.LeaveDetailsList.Count; i++)*@
        @*{*@
        <tr>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveType, new { @class = "ReadOnly", @readonly = "readonly", style = "width:80px; text-align:center" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveTaken, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveBalance, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveTotal, new { width = "100" })</td>
        </tr>
        <tr>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[1].LeaveType, new { @class = "ReadOnly", @readonly = "readonly", style = "width:80px; text-align:center" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[1].LeaveTaken, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[1].LeaveBalance, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => m.LeaveDetailsList[1].LeaveTotal, new { width = "100" })</td>
        </tr>
        @*}*@
    </table>
    <br />
    <table style="border:none">
        <tr>
            <td style="width:600px;text-align:left;border:none">
                <input id="btnSave" type="submit" value="Save" style="width:100px" />
            </td>
        </tr>
    </table>

}  
</div>

1 个答案:

答案 0 :(得分:0)

最简单的方法是在服务器上存储会话中所需的所有内容,然后在提交后从该会话中填充所需内容。