2013-04-10 84 views
0

我遇到了緩存問題,並嘗試了我可以找到的每個解決方案!MVC應用程序中的不需要的緩存

我有一個簡單的創建屏幕,在表中插入一行(儘管編輯現有行時我也遇到同樣的問題)。

創建該行時,用戶返回到前一個屏幕,該屏幕仍顯示舊數據。 (與編輯相同的問題)

刷新頁面沒有區別。差異瀏覽器有同樣的問題。數據已成功添加到數據庫中。只有通過重申應用程序才能刷新屏幕上的數據。

事情我曾嘗試:

1:

DataContext.Refresh(RefreshMode.OverwriteCurrentValues) 

2:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] 

3:

ModelState.Clear() 

其中沒有什麼不同。在編輯或創建之前我沒有遇到過這個問題,所以我必須錯過一些東西。任何幫助非常感謝!

以下是控制器的相關部分:

ISISDataContext db = new ISISDataContext(StudentISIS.Properties.Settings.Default.ISISConn.ToString()); 

    public ActionResult Index() 
    { 
     var student = (ISIS2Models.Student)Session["CurrentUser"]; 

     return View(student); 
    } 

    public ActionResult STGCreate(int id) 
    { 
     var enrolment = db.Enrolments.Single(e => e.EnrolmentID == id); 
     return View(enrolment); 
    } 


    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult STGCreate([Bind(Exclude = "Id")] StudentGrade STGToCreate, FormCollection collection) 
    { 
     var STG = new StudentGrade(); 

     STG.Grade = collection["StudentTG"]; 
     STG.EnrolmentID = int.Parse(collection["Enrolment"]); 
     STG.DateChanged = DateTime.Now; 

     db.StudentGrades.InsertOnSubmit(STG); 
     db.SubmitChanges(); 

     return RedirectToAction("Index"); 
    } 

編輯:

這裏是從循環通過入學以顯示級索引視圖的代碼:

<%foreach (var en in Model.Enrolments) {%> 
//Some table stuff 
<td> 
<%try 
     { %> 
     <%= Html.ActionLink(en.StudentGrades.Grade,"STGEdit",new {controller = "Progress", id = en.StudentGrades.StudentGradeID})%> 
     <%} 
     catch (NullReferenceException) {%><%= Html.ActionLink("Set","STGCreate",new {controller = "Progress", id = en.EnrolmentID})%><% } %> 
     </td> 
//Some more table stuff 

<%}% 

回答

1

行從哪裏來?是你的ISIS2Models.Student類嗎?我只能假設這是因爲您的Index方法中代碼太少。

如果是,並且您正在將其存儲在會話中,則不會更新該值,因此當您從Index中檢索該值時,它仍將具有相同的舊值。

你需要做的是每次打電話給Index時從數據庫中獲取更新的模型。像這樣的一些方法:

public ActionResult Index() 
{ 
    var currentUser = (ISIS2Models.Student)Session["CurrentUser"]; 

    var student = GetStudentById(currentUser.ID);//this will get the up-to-date student record from the DB 

    return View(student); 
} 
+0

它從表格鏈接獲得行的學生,即Student.Enrolments.StudentGrade。 如果是這樣的話,我該如何強制它刷新數據? – Chris 2013-04-10 10:50:10

+0

@Chris:哪裏?不在你的示例代碼中,它沒有。或者是你的'STGCreate'行動顯示不正確? – musefan 2013-04-10 10:51:03

+0

我編輯了這個問題,以包括循環訪問數據的索引頁上的代碼 – Chris 2013-04-10 10:57:13

相關問題