2013-04-10 86 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

相关问题