2010-10-06 71 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

相關問題