I am using one controller which is inserting values in the database. I want to display alert message from controller when the values insertesd in the database successfully. Is it possible. If yes then how?
我正在使用一个在数据库中插入值的控制器。我想在值成功插入数据库时显示来自控制器的警报消息。可能吗。如果是,那怎么样?
4 个解决方案
#1
3
Basically that depends on how are you inserting the value into the database, as you would need a method to tells you whether the insertion was successful. As there's a few ways to do that now, linq/entity framework/sql/etc.
基本上,这取决于您如何将值插入数据库,因为您需要一种方法来告诉您插入是否成功。因为现在有几种方法可以做到,linq / entity framework / sql / etc.
Then after you know whether did the insertion happens, then you can just assign a value to a variable and then from the code/aspx just check the value and do a simple alert.
然后在您知道插入是否发生之后,您可以只为变量赋值,然后从代码/ aspx中检查值并执行简单警报。
<script type="text/javascript">
//i'm using jquery ready event which will call the javascript chunk after the page has completed loading
$(document).ready(function(){
//assuming that your variable name from the code behind is bInsertSuccess
var bSuccess = "<%= bInsertSuccess %>";
if(bSuccess){
alert("Successfully Inserted");
}
});
</script>
#2
18
You can add the result to ViewData. For example:
您可以将结果添加到ViewData。例如:
if (SaveToDbOK)
{
ViewData["Success"] = "Data was saved successfully.";
// Do other things or return view
}
In your view you can place anywhere:
在您的视图中,您可以放在任何地方
MVC2:
MVC2:
<% if (ViewData["Success"] != null) { %> <div id="successMessage"> <%: ViewData["Success"] %> </div> <% } %>
MVC3:
MVC3:
@if (ViewData["Success"] != null) { <div id="successMessage"> @ViewData["Success"] </div> @}
I used this approach in my last project in order to make the information returned from the server unobtrusive. Checking whether ViewData["Success"] or ViewData["Failure"] are done in the Master page, the divs are formatted using CSS, jQuery code was used to hide the notifications after 5 seconds.
我在上一个项目中使用了这种方法,以便从服务器返回的信息不引人注目。检查ViewData [“Success”]或ViewData [“Failure”]是否在Master页面中完成,div使用CSS格式化,jQuery代码用于在5秒后隐藏通知。
Regards,
问候,
Huske
Huske
#3
5
public ActionResult UploadPropertyImage()
{
// Business logic....
return Content("<script language='javascript' type='text/javascript'>alert('Save Successfully');</script>");
}
#4
0
You may add below code to tell user
您可以添加以下代码来告诉用户
Return Content("Data added successfully");
#1
3
Basically that depends on how are you inserting the value into the database, as you would need a method to tells you whether the insertion was successful. As there's a few ways to do that now, linq/entity framework/sql/etc.
基本上,这取决于您如何将值插入数据库,因为您需要一种方法来告诉您插入是否成功。因为现在有几种方法可以做到,linq / entity framework / sql / etc.
Then after you know whether did the insertion happens, then you can just assign a value to a variable and then from the code/aspx just check the value and do a simple alert.
然后在您知道插入是否发生之后,您可以只为变量赋值,然后从代码/ aspx中检查值并执行简单警报。
<script type="text/javascript">
//i'm using jquery ready event which will call the javascript chunk after the page has completed loading
$(document).ready(function(){
//assuming that your variable name from the code behind is bInsertSuccess
var bSuccess = "<%= bInsertSuccess %>";
if(bSuccess){
alert("Successfully Inserted");
}
});
</script>
#2
18
You can add the result to ViewData. For example:
您可以将结果添加到ViewData。例如:
if (SaveToDbOK)
{
ViewData["Success"] = "Data was saved successfully.";
// Do other things or return view
}
In your view you can place anywhere:
在您的视图中,您可以放在任何地方
MVC2:
MVC2:
<% if (ViewData["Success"] != null) { %> <div id="successMessage"> <%: ViewData["Success"] %> </div> <% } %>
MVC3:
MVC3:
@if (ViewData["Success"] != null) { <div id="successMessage"> @ViewData["Success"] </div> @}
I used this approach in my last project in order to make the information returned from the server unobtrusive. Checking whether ViewData["Success"] or ViewData["Failure"] are done in the Master page, the divs are formatted using CSS, jQuery code was used to hide the notifications after 5 seconds.
我在上一个项目中使用了这种方法,以便从服务器返回的信息不引人注目。检查ViewData [“Success”]或ViewData [“Failure”]是否在Master页面中完成,div使用CSS格式化,jQuery代码用于在5秒后隐藏通知。
Regards,
问候,
Huske
Huske
#3
5
public ActionResult UploadPropertyImage()
{
// Business logic....
return Content("<script language='javascript' type='text/javascript'>alert('Save Successfully');</script>");
}
#4
0
You may add below code to tell user
您可以添加以下代码来告诉用户
Return Content("Data added successfully");