多个TempData变量传递给同一个动作

时间:2015-04-22 16:51:44

标签: c# entity-framework asp.net-mvc-5 ef-code-first

我正在使用MVC和Entity Framework创建ASP.NET Web应用程序。我有两个不同的成功消息,当用户进入或退出时,我将传递给索引操作。当用户进入时,成功消息将正确打印,但是当用户出于某种原因退出时则不会。

动作非常相似,我使用了所有相同的约定,所以我无法弄清楚为什么会打印而另一个不打印。我已经尝试过调试,没有红旗,数据库中的所有内容都应该更新。是否无法将多个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>

1 个答案:

答案 0 :(得分:0)

一旦你阅读了TempData就丢失了,所以你需要做一个Peek或Keep:

ViewBag.ClockInSuccess = TempData.Peek("ClockInSuccess");
ViewBag.ClockOutSuccess = TempData.Peek("ClockOutSuccess");

http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion