i'm trying to figure out how i can display a message after a user has successfully insert/update/delete a record in the database. i've seen so many MVC related information that uses tempdata but nothing outside of mvc.
i'm coming from PHP and codeigniter and they use flashdata() which uses sessions. once it displays it, it gets destroyed, that way if the page gets refreshed, the message is gone. is there anything razor has that works similar? my code is something like
if (IsPost) {
// foo = Request.Form["bar"];
if (Validation.IsValid()) {
// do my database stuff
// redirect to a page
Response.Redirect("~/some-page");
}
}
You could handle this all by using a Javascript alert and handling the navigation through Javascript itself by writing your alert using Response.Write :
Response.Write("<script>alert('The update was successful!');window.location='url';</script>");
If you are going to use a relative URL like in your example, you'll want to use something like ResolveUrl to store the proper URL in a string and then pass that into the <script> within your Response.Write.
Full Example
if(IsPostBack)
{
//If Validation passes, perform your action
if(Validation.IsValid())
{
//Perform your Database Query
//Resolve your target URL that you want to Redirect to (if you wanted to store it in a variable)
string url = ResolveUrl("~/Some-Page.aspx");
//Display a Javascript alert indicating that the action was successful and navigate afterwards
Response.Write(String.Format("<script>alert('The update was successful!');window.location='{0}';</script>",url));
}
}
@rion thanks for the reply but i didn't want to use the alert(). i'm hoping for something similar to flashdata() from codeigniter where a session is created, displayed, then destroyed right afterwards. is there something built-in or do i have to create one
from scratch or is it handled differently in razor?
The closest equivalent to flashdata would to be to use
TempData, which is a temporary storage component that has a very short lifespan (typically long enough to be accessed once, then it is destroyed) but it is only available within MVC.
I suppose you could use the Session to temporarily store the value that you want to display and manually dispose of it after you have used it.
if(IsPostBack)
{
//If Validation passes, perform your action
if(Validation.IsValid())
{
//Perform your Database Query
//Store your message within the Session (accessed through Session["YourMessage"])
Session["YourMessage"] = "Update was successful";
//Redirect to your page
Response.Redirect("~/some-page");
}
}
You can remove the value from the Session by using the Session.Remove() method :
is there something built-in or do i have to create one from scratch or is it handled differently in razor?
The is no equivalent to FlashData in the ASP.NET Web Pages (Razor) framework so you will have to implement your own mechanism if you want that functionality.
if (IsPost) {
// foo = Request.Form["bar"];
if (Validation.IsValid()) {
// do my database stuff
// redirect to a page
Session["flashdata_error"] = "ERROR: Unable to create new record.";
Response.Redirect("~/some-page");
}
}
i just wished there was something built-in. i could use a variable and have it displayed on the page w/o redirect but if the user refreshes the browser it could mean duplicate records or unwanted errors. i tried to learn mvc 4 but i didn't want to use scaffolding
too much. the problem is the library is huge and i always get a "cannot find object or namespace... blah blah error". i end up spending so much time googling. razor framework is much more straight forward for me. the helpers in mvc aren't much help either.
i just wanted to add html attributes by using new object {@class="my-own-css"}. it's not always that easy. there's times i had to create a template for an object... anyways i'm blabbering now. thanks for the help. good thing i'm only curious about the language
and not have to build a production app.
lambanog
Member
7 Points
11 Posts
notification message after a successful database query
Feb 21, 2013 12:43 AM|LINK
i'm trying to figure out how i can display a message after a user has successfully insert/update/delete a record in the database. i've seen so many MVC related information that uses tempdata but nothing outside of mvc.
i'm coming from PHP and codeigniter and they use flashdata() which uses sessions. once it displays it, it gets destroyed, that way if the page gets refreshed, the message is gone. is there anything razor has that works similar? my code is something like
if (IsPost) { // foo = Request.Form["bar"]; if (Validation.IsValid()) { // do my database stuff // redirect to a page Response.Redirect("~/some-page"); } }please help, thanks.
Rion William...
All-Star
27174 Points
4508 Posts
Re: notification message after a successful database query
Feb 21, 2013 01:17 AM|LINK
You could handle this all by using a Javascript alert and handling the navigation through Javascript itself by writing your alert using Response.Write :
Response.Write("<script>alert('The update was successful!');window.location='url';</script>");If you are going to use a relative URL like in your example, you'll want to use something like ResolveUrl to store the proper URL in a string and then pass that into the <script> within your Response.Write.
Full Example
if(IsPostBack) { //If Validation passes, perform your action if(Validation.IsValid()) { //Perform your Database Query //Resolve your target URL that you want to Redirect to (if you wanted to store it in a variable) string url = ResolveUrl("~/Some-Page.aspx"); //Display a Javascript alert indicating that the action was successful and navigate afterwards Response.Write(String.Format("<script>alert('The update was successful!');window.location='{0}';</script>",url)); } }lambanog
Member
7 Points
11 Posts
Re: notification message after a successful database query
Feb 21, 2013 01:43 AM|LINK
@rion thanks for the reply but i didn't want to use the alert(). i'm hoping for something similar to flashdata() from codeigniter where a session is created, displayed, then destroyed right afterwards. is there something built-in or do i have to create one from scratch or is it handled differently in razor?
Rion William...
All-Star
27174 Points
4508 Posts
Re: notification message after a successful database query
Feb 21, 2013 02:06 AM|LINK
The closest equivalent to flashdata would to be to use TempData, which is a temporary storage component that has a very short lifespan (typically long enough to be accessed once, then it is destroyed) but it is only available within MVC.
I suppose you could use the Session to temporarily store the value that you want to display and manually dispose of it after you have used it.
if(IsPostBack) { //If Validation passes, perform your action if(Validation.IsValid()) { //Perform your Database Query //Store your message within the Session (accessed through Session["YourMessage"]) Session["YourMessage"] = "Update was successful"; //Redirect to your page Response.Redirect("~/some-page"); } }You can remove the value from the Session by using the Session.Remove() method :
Session.Remove("YourMessage");Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: notification message after a successful database query
Feb 21, 2013 07:03 AM|LINK
The is no equivalent to FlashData in the ASP.NET Web Pages (Razor) framework so you will have to implement your own mechanism if you want that functionality.
In the meantime, you have a number of other options for persisting data across requests: http://www.mikesdotnetting.com/Article/192/Transferring-Data-Between-ASP.NET-Web-Pages
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
lambanog
Member
7 Points
11 Posts
Re: notification message after a successful database query
Feb 22, 2013 09:44 PM|LINK
so i ended up with the following in my _layout.cshtml page
@if (Session["flashdata_error"] != null) { <div class="alert alert-success"> <p>@Session["flashdata_error"].ToString();</p> </div> Session["flashdata_error"] = null; }then ...
if (IsPost) { // foo = Request.Form["bar"]; if (Validation.IsValid()) { // do my database stuff // redirect to a page Session["flashdata_error"] = "ERROR: Unable to create new record."; Response.Redirect("~/some-page"); } }i just wished there was something built-in. i could use a variable and have it displayed on the page w/o redirect but if the user refreshes the browser it could mean duplicate records or unwanted errors. i tried to learn mvc 4 but i didn't want to use scaffolding too much. the problem is the library is huge and i always get a "cannot find object or namespace... blah blah error". i end up spending so much time googling. razor framework is much more straight forward for me. the helpers in mvc aren't much help either. i just wanted to add html attributes by using new object {@class="my-own-css"}. it's not always that easy. there's times i had to create a template for an object... anyways i'm blabbering now. thanks for the help. good thing i'm only curious about the language and not have to build a production app.