2010-10-06 70 views
0

我使用我的视图在数据库表中添加记录。 我点击提交后,我希望屏幕清除准备好我的下一个插入。 因此,在我的控制器中,我发送了一个新的ViewModel,其中包含我的HttpGet和HttpPost的默认值。 但是我发现,一旦我插入了一条记录,所有旧值都会保留在我的HttpPost后面。 我在做什么错?我刚刚插入了一项数据。现在我想清除我的视图,但它不会

[HttpGet] 
    [Authorize(Roles = "Administrator, AdminAccounts")] 
    public ActionResult Add() 
    { 
     ViewData["LastPerson"] = string.Empty; 

     return View(new EmployeeViewModel()); 
    } 

    [HttpPost] 
    [Authorize(Roles = "Administrator, AdminAccounts")] 
    public ActionResult Add(Employee employee) 
    { 
     if (ModelState.IsValid) 
     { 
      var employeeViewModel = new EmployeeViewModel(); 
      ViewData["LastPerson"] = string.Format("{0} {1} {2}", employee.Forename, employee.Middlenames, 
            employee.Surname); 
      employeeViewModel.Employee = employee; 
      employeeViewModel.Employee.Add(); 
     } 
     return View(new EmployeeViewModel()); 
    } 

我的视图看起来像这样;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" 
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Add 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server"> 

    <% using (Html.BeginForm("Add", "Employee")) {%> 
     <%: Html.ValidationSummary(true) %> 

     <fieldset> 
      <legend>Add Employee</legend> 
      <table> 
       <tr> 
        <td align="right"> 
       <%: Html.LabelFor(model => model.Employee.UserName)%> 
        </td>      
        <td> 
       <%: Html.EditorFor(model => model.Employee.UserName)%> 
       <%: Html.ValidationMessageFor(model => model.Employee.UserName)%> 
        </td> 
       </tr> 
       <tr> 
        <td align="right"> 
       <%: Html.LabelFor(model => model.Division.DivisionId) %> 
        </td>      
        <td> 
       <%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->")%> 
       <%: Html.ValidationMessageFor(model => model.Division.DivisionId)%> 
        </td> 
       </tr> 
       <tr> 
        <td align="right"> 
       <%: Html.LabelFor(model => model.Department.DepartmentId) %> 
        </td>      
        <td> 
       <%: Html.DropDownListFor(model => model.Department.DepartmentId, Model.DepartmentSelectList, "<--Select-->")%> 
       <%: Html.ValidationMessageFor(model => model.Department.DepartmentId) %> 
        </td> 
       </tr> 

       <tr> 
        <td align="center" colspan="2" style="padding-top:20px;"> 
        <input type="submit" value="Save" /></td> 
       </tr> 
      </table> 
      <% if (ViewData["LastPerson"].ToString().Length > 0) 
       { %> 
       <p> 
        At time <% Response.Write(DateTime.Now.ToString("T")); %> 
        - You have just entered <%Response.Write(ViewData["LastPerson"].ToString()); %>. 
       </p> 
      <%} %> 
     </fieldset> 

    <% } %> 

    <div> 
     <%: Html.ActionLink("Back to List", "Index") %> 
    </div> 

    <script language="javascript" type="text/javascript"> 

     //Hook onto the DivisionId list's onchange event 
     $("#Division_DivisionId").change(function() { 
      //build the request url 
      var url = '<%: Url.Content("~/")%>' + "Employee/GetDepartments"; 
      //fire off the request, passing it the id which is the DivisionId's selected item value 
      $.getJSON(url, { divisionId: $("#Division_DivisionId").val() }, function (data) { 
       //Clear the Department list 
       $("#Department_DepartmentId").empty(); 
       $("#Department_DepartmentId").append("<option><--Select--></option>"); 
       //Foreach Department in the list, add a department option from the data returned 
       $.each(data, function (index, optionData) { 
        $("#Department_DepartmentId").append(
        "<option value='" + optionData.DepartmentId + "'>" + 
         optionData.DepartmentName + "</option>"); 
       }); 
      }); 
     }).change(); 

    </script> 

回答

2

ModelState可以让您惊讶于您最终记忆的数据。然而,在你的情况,你应该重定向到您的空白页,而不是直接返回View(model)

return RedirectToAction("Add"); 

相反的:

return View(new EmployeeViewModel()); 

提交表格后,如果用户刷新时会发生什么浏览器?使用您的设计,它会重新发回表单数据,这并不好。重定向将解决这个问题。请参阅此处了解更多信息:http://en.wikipedia.org/wiki/Post/Redirect/Get

+0

@ arame3333,只需将'LastPerson'作为查询字符串参数,并在调用RedirectToAction作为路由值时传递该值:'RedirectToAction(“Add”,new {lastPerson = ViewData [“LastPerson”]});'Then你可以在其他控制器操作'Add'中使用该值,方法是向其中添加其他参数(最好使用默认值,即使未设置该值,也可以使用页面的默认值)。 – 2010-10-06 15:06:45

+0

谢谢你,这是一个更好的解决方案比我尝试的那个要好。 – arame3333 2010-10-06 16:19:52

相关问题