0

我有一个根据特定 ID 显示信息的视图。

我的控制器是:

 public ActionResult ModifyWorkout(string WorkoutId, bool? Status)
    {
        Guid pkWorkoutId = new Guid(WorkoutId);
        BLLWorkouts _objBLLWorkouts = new BLLWorkouts();
        var WorkoutRow = _objBLLWorkouts.GetOneWorkout(pkWorkoutId);
        WorkoutModel PageModel = new WorkoutModel
        {
            pkWorkoutId = WorkoutRow.pkWorkoutId,
            WorkoutName = WorkoutRow.WorkoutName,
            WorkoutNote = WorkoutRow.WorkoutNote,
            Sessions = WorkoutRow.Sessions,
            CmpFlag = WorkoutRow.CmpFlag,
            tblWorkoutSessions = WorkoutRow.tblWorkoutSessions
        };
        ViewBag.UpdateStatus = Status;
        return View(PageModel);
    }

我总是传递一个查询字符串作为锻炼 id 来获取一条记录。但是,当我更新该记录时,它会发布到另一个操作:

 public ActionResult UpdateWorkout(WorkoutModel model)
    {
        bool _status = false;
        BLLWorkouts _objBLLWorkouts = new BLLWorkouts();
        tblWorkout _row = new tblWorkout();
        _row.WorkoutName = model.WorkoutName;
        _row.WorkoutNote = model.WorkoutNote;
        _row.Sessions = model.Sessions;
        _row.pkWorkoutId = model.pkWorkoutId;
        _row.CmpFlag = model.CmpFlag;
        _status = _objBLLWorkouts.UpdateWorkout(_row);

        return RedirectToAction("ModifyWorkout", new { WorkoutId = model.pkWorkoutId, Status = _status });

    }

现在的问题是,更新我的网址后变成这样:

/ModifyWorkout?WorkoutId=438b6828-1a21-4ad0-9e40-485dda75b1f6&Status=true"

由于&Status=true在 Url 中,即使在刷新页面后也会显示状态消息。

<div >
            @if (ViewBag.UpdateStatus != null)
            {
                if (ViewBag.UpdateStatus)
                {
                <span class="successmsgCommon">Workout updated successfully.</span>
                }
                else
                {
                <span class="errormsgCommon">Workout was not updated. Please try again.</span>
                }
            }

        </div>

有没有办法解决这个问题?

4

2 回答 2

2

用于TempData发送值

  TempData["Status"] = true; 

消耗

 if (TempData["Status"])

关于TempData

  • TempData 用于将数据从当前请求传递到后续请求,以防重定向。
  • TempData 是一个字典对象,它派生自 TempDataDictionary 类并存储在短期会话中
于 2013-08-12T11:24:47.493 回答
1

您可以为此使用 TempData:

TempData["Status"] = _status;
return RedirectToAction("ModifyWorkout", new { WorkoutId = model.pkWorkoutId });

ASP.NET MVC 将为下一个请求存储此值,以便您可以在重定向操作中读取它。

if ((bool)TempData["Status"])
{
    <span class="successmsgCommon">Workout updated successfully.</span>
}
else
{
    <span class="errormsgCommon">Workout was not updated. Please try again.</span>
}
于 2013-08-12T11:23:50.967 回答