[英]How to show a “Saved!” message after Redirect() is made..?
I am currently working in a MVC4 project. 我目前正在从事MVC4项目。
Imagine the following scenario: 想象一下以下场景:
(1) The user is allowed to add data to multiple tables and after that needs to confirm by hitting the Submit. (1)允许用户向多个表添加数据,之后需要通过点击提交进行确认。 <input type="submit" value="" id="saveBtn" title="Save" />
The reason there is no value is because I have CSS applied to the Submit. <input type="submit" value="" id="saveBtn" title="Save" />
没有值的原因是因为我将CSS应用于提交。
(2) After that, to avoid a previous bug to prevent a refresh of the page to duplicate data, I need to Redirect the user in my Controller: return Redirect("Edit?Id=" + id);
(2)之后,为避免以前的错误阻止刷新页面重复数据,我需要在我的Controller中重定向用户: return Redirect("Edit?Id=" + id);
(3) After the Redirect() is made, I want to show a simple message that says "Saved!" (3)完成Redirect( )之后, 我想显示一条简单的消息“Saved!” with a delay of 5 seconds . 延迟5秒 。 I do not want an alert("Saved!");
我不想要alert("Saved!");
. 。
I currently got a pseudo-solution working but not preferred. 我目前有一个伪解决方案工作,但不是首选。 It's an simple click function in jQuery: 这是jQuery中的一个简单点击功能:
CSS CSS
.successMessage {
color: darkgreen;
}
HTML HTML
<div class="successMessage" style="display: none;">Saved!</div>
JS JS
$('#saveBtn').click(function () {
$(".successMessage").delay(500).show(0);
});
The problem is that, since I Redirect();
问题是,因为我Redirect();
the user, my message cannot be delayed with this "solution". 用户,我的消息不能延迟这个“解决方案”。 And after a Redirect();
并在Redirect();
is made, the message will not show up again. 制作完成后,该消息将不再显示。
What is a better approach in order to get a 'delayed' message? 获得“延迟”消息的更好方法是什么? Thanks in advance. 提前致谢。
Add a TempData in your submit action,and show the delay message in your view by confirm if TempData has value when page is ready. 在提交操作中添加TempData,并在页面准备好时确认TempData是否具有值,从而在视图中显示延迟消息。
Action: 行动:
//save info
TempData["Message"]="Saved!";
return Redirect("Edit?Id=" + id)
View: 视图:
string message=TempData["Message"]==null?"":TempData["Message"].ToString();
@if(message!="")
{
<div class="successMessage" style="display: none;">@message</div>
<script type='text/javascript'>
$(document).ready(function(){
$(".successMessage").delay(500).show(0);
})
</script>
}
The best solution to show notification messages is to use TempData["Message"]
to store values and on every page do this- 显示通知消息的最佳解决方案是使用TempData["Message"]
来存储值,并在每个页面上执行此操作 -
if (TempData["Message"] != null)
{
ViewBag.Message = TempData["Message"];
}
Now in the View, 现在在视图中,
<div>
@if (ViewData["Message"] != null)
{
<div>Saved!</div>
}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.