I use a JavaScript library called Alertify to show simple, pretty messages, like "note created", or whatever. Often I want to send the user to a new page and then show an Alertify message.
I'm trying to find a way to do this on the fly, without tons of configuration.
Currently I do this by creating an AlertifyMessages
object:
public class AlertifyMessages
{
public string LogMessage { get; set; }
public string LogCallback { get; set; }
public string SuccessMessage { get; set; }
public string SuccessCallback { get; set; }
public string ErrorMessage { get; set; }
public string ErrorCallback { get; set; }
public AlertifyMessages() { }
public AlertifyMessages(string logMessage = null, string logCallback = null,
string successMessage = null, string successCallback = null,
string errorMessage = null, string errorCallback = null)
{
LogMessage = logMessage;
LogCallback = logCallback;
SuccessMessage = successMessage;
SuccessCallback = successCallback;
ErrorMessage = errorMessage;
ErrorCallback = errorCallback;
}
}
and passing it to an action method:
public ActionResult Test()
{
// RedirectToAction([action], [controller], [routeValues])
return RedirectToAction("Details", "Notes",
new AlertifyMessages(logMessage: "Lorem ipsum"));
}
and in the destination action method:
public class NotesController : Controller
{
// GET
public ActionResult Details(AlertifyMessages alertify)
{
return View(new NoteDetailsViewModel(alertify));
}
}
which, finally, displays the messages in the view:
@if (Model.Alertify.LogMessage != null)
{
@:@Html.Alertify("log", Model.Alertify.LogMessage, Model.Alertify.LogCallback)
}
@if (Model.Alertify.SuccessMessage != null)
{
@:@Html.Alertify("success", Model.Alertify.SuccessMessage, Model.Alertify.SuccessCallback)
}
@if (Model.Alertify.ErrorMessage != null)
{
@:@Html.Alertify("error", Model.Alertify.ErrorMessage, Model.Alertify.ErrorCallback)
}
The helper method Alertify
just adds the JavaScript to call alertify.js, but you can see it here
Besides being a nightmare in so many ways, this doesn't even work unless the AlertifyMessage
is all I need to pass. I could make some kind of complicated ToString()
override to resolve this, but otherwise attempting to pass, say, an ID as well as an AlertifyMessages
like so
return RedirectToAction("Details", "Notes",
new { id = 1, alertify = new AlertifyMessages(logMessage: "Lorem ipsum")});
yields a URL like
https://localhost/Notes/Details/1?alertify=Project.Namespace.AlertifyMessages
This is way too complicated. There must be a better way.
Edit: I'd like a way to specify the message in the calling action method (the one performing the redirect), and nowhere else. Is there a way to do this other than adding code to every single method and view?
return RedirectToAction("Details", "Notes", new { id = 1, LogMessage = "...", LogCallback = "...", ErrorCallback = "..." });
\$\endgroup\$TempData
(although the message will not be displayed again if the user refreshes the browser, sinceTempData
lasts only one request) \$\endgroup\$