2012-03-07 31 views
0

を更新していないユーザーが編集フォームを提出した後、オブジェクトのためのこれらの値は、モデルのサーバー側に保存されますが、以前の値は、ビューに表示されます。MVC ModelState.Clearは私のMVCプログラムでにModelState

私はMVCの検証プロセスと関係があることを知っています。このプロセスでは、サーバー側の値より前にModelStateを最初にチェックします。私がフォーラムで読んだ解決策は、ModelStateをクリアすることです。唯一の問題は、ModelState.Clearが私のために働いていないことです。

お願いします。

モデル

public class Help 
    { 
     [HiddenInput(DisplayValue=true)] 
     public int HelpID { get; set; } 

     [Required(ErrorMessage = "Please enter a proper URL")] 
     public string URL { get; set; } 

     [Required(ErrorMessage = "Please enter a content description:")] 
     [DataType(DataType.MultilineText)] 
     public string HelpContent { get; set; } 

     /*? 2 properites are nullable*/ 
     public DateTime? createDateTime { get; set; } 
     public DateTime? modifiedDateTime { get; set; }   
    } 

コントローラ

/*Create the admin controller*/ 
    public class AdminController : Controller 
    { 
     //declare interface object 
     private IHelpRepository repository; 

     /*Pass a db interface to controller*/ 
     public AdminController(IHelpRepository repo) 
     { 
      repository = repo; 
     } 

     /*default admin screen. displays help table obs*/ 
     public ViewResult Index() 
     {    
      return View(); 
     } 

     /*Returns add view form*/ 
     public ActionResult AddForm() 
     {    
      return PartialView();    
     } 

     /*Will handle the post for the add screen after user has 
     submitted add information*/ 
     [HttpPost] 
     [ValidateInput(false)] //this allows admin to place html in field 
     public ActionResult AddForm(Help help) 
     {   
      if (ModelState.IsValid) //if all fields are validated 
      {     
       //set the edit date 
       help.createDateTime = DateTime.Now; 
       repository.SaveHelp(help);     
       return (null); //return "null" to div so control is given back to main view 
      } 
      else //there is something wrong. send back to view    
      { 
       return PartialView(help); 
      } 
     } 

     /*Returns edit view form, searches for object to edit with id 
     if no id provided, 0 by default*/ 
     public ActionResult EditForm(int helpID = 0) 
     { 
      Help help = repository.Help.FirstOrDefault(q => q.HelpID == helpID); 
      help.HelpContent = System.Web.HttpUtility.HtmlDecode(help.HelpContent); 

      return PartialView(help); 
     } 

     /*Will handle the post for the edit screen after user has 
     submitted edit information*/ 
     [HttpPost] 
     [ValidateInput(false)] //this allows admin to place html in field 
     public ActionResult EditForm(Help help) 
     { 



      if (ModelState.IsValid) //if all fields are validated 
      { 
       //set the edit date 
       help.modifiedDateTime = DateTime.Now; 
       repository.SaveHelp(help); 
       ModelState.Clear(); 
       return (null); //return "null" to div so control is given back to main view 
      } 
      else //there is something wrong. send back to view    
      { 

       return PartialView(help); 
      } 
     } 

     /*Delete action method, searches with id*/ 
     [HttpPost] 
     public ActionResult Delete(int helpId) 
     { 
      Help helpDel = repository.Help.FirstOrDefault(p => p.HelpID == helpId); 
      if (helpDel != null) //if the object is found, delete 
      { 
       repository.DeleteHelp(helpDel); 
      } 

      //in all cases return to index 
      return RedirectToAction("Index"); 
     } 

     /*Used by the telerik table to rebind grid*/ 
     [GridAction] 
     public ActionResult AjaxBinding() 
     { 
      return View(new GridModel(repository.Help)); 
     } 
    }//end admin controller class` 

部分ビュー(div要素にロードされる) `

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Editx" })) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Edit Entry</legend> 
     @Html.HiddenFor(model => model.HelpID) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.URL) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.URL) 
      @Html.ValidationMessageFor(model => model.URL) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.HelpContent, "Help Content")    
     </div> 
     <div class="editor-field"> 
     @{ 
    Html.Telerik().EditorFor(content => content.HelpContent) 
     .Name("HelpContent") 
     .FileBrowser(settings => settings 
      .Browse("Browse", "ImageBrowser") 
      .Thumbnail("Thumbnail", "ImageBrowser") 
      .Upload("Upload", "ImageBrowser") 
      .DeleteFile("DeleteFile", "ImageBrowser") 
      .DeleteDirectory("DeleteDirectory", "ImageBrowser") 
      .CreateDirectory("CreateDirectory", "ImageBrowser") 
    ) 
     .Render(); 
     } 
     @Html.ValidationMessageFor(model => model.HelpContent) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.createDateTime, "Create Date") 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.createDateTime) 
      @Html.ValidationMessageFor(model => model.createDateTime) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.modifiedDateTime, "Modified Date") 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.modifiedDateTime) 
      @Html.ValidationMessageFor(model => model.modifiedDateTime) 
     </div> 
     <p> 
      <input id="btnEdit" type="submit" value="Save" /> 
      <button id="btnCancel">Cancel</button> 
     </p> 
    </fieldset> 
    } 

答えて

0

約100リンクを回った後、私は自分の問題を解決しました。 ModelState.Clearはコントローラ内のオブジェクトの値を実際にクリアしますが、何らかの理由でビューに古い値が表示されています。おそらく私はdivタグに私の編集フォームをロード/アンロードするので?聞かないでください。知りません。私のために働く解決策はこれです:

$.ajax({ 
url: "somecontroller/someAction, 
cache: false, // this is key to make sure JQUERY does not cache your request 
success: function(data) { 
alert(data); } }); 

"キャッシュ"設定を "false"に設定する必要がありました。

解決策のために@ minus4のおかげで、bruh。

+0

どこに追加しましたか? jQuery.jsに? – SteveCav

関連する問題