2015-04-22 65 views
0

我使用MVC和實體框架創建ASP.NET Web應用程序。我有兩個不同的成功消息,我將它們傳遞給索引操作,以便用戶進入或退出時。成功消息將在用戶加入時正確打印,但當用戶由於某種原因退出時不會打印。傳遞給同一個動作的多個TempData變量

這些操作非常相似,我使用了所有相同的約定,所以我無法弄清楚爲什麼要打印,而另一個不會。我試過調試,並沒有紅旗和數據庫中的所有更新。是不可能將多個TempData變量傳遞給同一個動作?

下面是相關代碼:

控制器

 // GET: TimeClocks 
    public ActionResult Index() 
    { 
     ViewBag.ClockInSuccess = TempData["ClockInSuccess"]; 
     ViewBag.ClockOutSuccess = TempData["ClockOutSuccess"]; 
     return View(); 
    } 
    [HttpPost] 
    public ActionResult ClockIn(TimeClock timeClock) 
    { 
     if(db.TimeClocks.ToList().Count == 1) 
     { 
      ModelState.AddModelError("ExistsError", "You already clocked in at" + timeClock.ClockIn); 
     } 
     string currentUserId = User.Identity.GetUserId(); 
     ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); 
     timeClock.ApplicationUser = currentUser; 
     timeClock.ClockIn = DateTime.Now; 

     if (ModelState.IsValid) 
     { 
      db.TimeClocks.RemoveRange(db.TimeClocks.ToList()); 
      db.TimeClocks.Add(timeClock); 
      db.SaveChanges(); 
      TempData["ClockInSuccess"] = "You clocked in successfully at " + timeClock.ClockIn; 
      return RedirectToAction("Index"); 
     } 

     return RedirectToAction("Index", timeClock); 
    } 
    [HttpPost] 
    public ActionResult ClockOut(TimeClock timeClock) 
    { 
     timeClock = db.TimeClocks.FirstOrDefault(); 
     if(timeClock.ClockIn == null) 
     { 
      ModelState.AddModelError("NullError", "You must clock in before you can clock out."); 
      return RedirectToAction("Index"); 
     } 
     timeClock.ClockOut = DateTime.Now; 

     if (ModelState.IsValid) 
     { 
      db.Entry(timeClock).State = EntityState.Modified; 
      db.SaveChanges(); 
      TempData["ClockOutSuccess"] = "You clocked out successfully at " + timeClock.ClockOut; 
      return RedirectToAction("Index"); 
     } 
     return RedirectToAction("Index", timeClock); 
    }`` 

查看

@model FinalProject.Models.TimeClock 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Employee Time Clock</h2> 

@using (Html.BeginForm("ClockIn", "TimeClocks")) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Clock In" class="btn btn-lg" /> 
      </div> 
     </div> 
    </div> 
} 
@using (Html.BeginForm("ClockOut", "TimeClocks")) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Clock Out" class="btn btn-lg" /> 
      </div> 
     </div> 
    </div> 
} 
@{ 
    if (@ViewBag.ClockInSuccess != "") 
    { 
     <p class="alert-success">@ViewBag.ClockInSuccess</p> 
    } 
    else if (@ViewBag.ClockOutSuccess != "") 
    { 
     <p class="alert-success">@ViewBag.ClockOutSuccess</p> 
    } 
} 


<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
+0

你的代碼沒有意義。你的觀點有@ @ Html.ActionLink(「返回列表」,「索引」),暗示你顯示的視圖不是'索引'視圖。在你的post方法中,如果模型無效,那麼你調用'return RedirectToAction(「Index」,timeClock);'但是Index()方法不接受一個參數,所以第二個參數沒有意義(不是說它會起作用)。非常不清楚你想要做什麼以及你期望結果如何。 –

回答