2012-03-15 6 views
0

ASP.NET MVC Webアプリケーションへの同時アクセスを処理したいので、timestampアノテーションを追加しました。オブジェクトを編集アクションメソッドのパラメータとして渡す場合、オブジェクトのバインドインクルードリストを定義する方法

[HttpPost] 
     public ActionResult Edit(Assessment a) 
     {   try 
      { 
       if (ModelState.IsValid) 
       { 
        elearningrepository.UpdateAssessment(a); 
        elearningrepository.Save(); 
        return RedirectToAction("Details", new { id = a.AssessmentID }); 
       } 
      } 
      catch (DbUpdateConcurrencyException ex) 
      { 
       var entry = ex.Entries.Single(); 
       var clientValues = (Assessment)entry.Entity; 

      ModelState.AddModelError(string.Empty, "The record you attempted to edit was" 
       + "modified by another user after you got the original value."); 
           } 
      catch (DataException) 
      { 
       ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator."); 
      }  
      return View(a);} 
- :私はfolloiwngが含まれているアクションメソッドに続いて

[MetadataType(typeof(Assessment_Validation))] 
[Bind(Include = "Date, Title")] 
    public partial class Assessment {} 

&

public class Assessment_Validation : IValidatableObject 
    { public DateTime Date { get; set; } 

      [Required(ErrorMessage = " Assessment Title is Rquired")] 
      public String Title { get; set; } 

      [Timestamp] 
      public Byte[] Timestamp { get; set; } } 

- :以下のように私のクラスへ

し、最終的にelearningrepository.UpdateAssessment(a): -

public void UpdateAssessment(Assessment a) 
     { 
      entities1.Entry(a).State = EntityState.Modified; 
     } 

上記2人のユーザが同じオブジェクトを編集していない場合でも、常にDbUpdateConcurrencyExceptionを返します、私は、これは部分クラスで[Bind(Include = "Date, Title")]に関連していると思います。ですから、私は編集アクションメソッドで引数としてオブジェクトを渡すことができ、同時にオブジェクトのリストをBindと定義しますか? 私は(オブジェクトをパラメータとして持っていないように)私のアクションメソッドは、次のように更新した場合もproblme: - 、そしてDbUpdateConcurrencyExceptionが提起されることはありません!!!!: -

[HttpPost] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      Assessment a = elearningrepository.GetAssessment(id); 
          try 
      { 
       if (TryUpdateModel(a)) 
       { 
        elearningrepository.UpdateAssessment(a); 
        elearningrepository.Save(); 
        return RedirectToAction("Details", new { id = a.AssessmentID }); 
       } 
      } 
      catch (DbUpdateConcurrencyException ex) 
      { 
       var entry = ex.Entries.Single(); 
       var clientValues = (Assessment)entry.Entity; 

      ModelState.AddModelError(string.Empty, "The record you attempted to edit was" 
       + "modified by another user after you got the original value."); 
      } 
      catch (DataException) 
      {    ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator."); 
      }return View(a); 

ので、私は解決することができますかこのプロブラム?

答えて

0

Timestamp属性のモデルプロパティをデコレートしましたか?

関連する問題