Fork me on GitHub
Blog
The journal that this archive was targeting has been deleted. Please update your configuration.
Main | Catch and Report Behavior Exceptions »
Monday
Oct152012

Report a Message using a ViewModel

At times, you just want to display an informational message to the end user when something happens in you view. You can choose to display the message as part of your view or you can let the controller handle the displaying of message. To do so, you need to first create a ViewModel that represents a message, then send a behavior event from your view containing an instance of MessageViewModel, catch it from the controller and then report it to the end user (by displaying it as a modal message perhaps):

ViewModel that wraps the informational message:
public partial class MessageViewModel
{
    public string Identification { get; set; }
    public string Tittle { get; set; }
    public string Message { get; set; }
    public string Details { get; set; }
}
How you would send your message from your view:
this.SendBehaviorEvent(
    new BehaviorEvent<MessageViewModel>()
    {
        BehaviorReference = BehaviorReference.MessageDisplayBehavior,
        ViewModel = new MessageViewModel()
                        {
                            Message =
                                "Please accept terms and conditions.",
                            Tittle = "Warning"
                        }
    });
Catch your MessageViewModel from your controller and then display it:
public partial class Controller
{
    //Catching it before it gets executed
    protected override void OnBeforeBehaviorEvent<T>(
        IBehaviorEventSender<T> sender, 
        BehaviorEvent<T> behaviorEvent)
    {
        base.OnBeforeBehaviorEvent<T>(sender, behaviorEvent);

        if(behaviorEvent.BehaviorReference 
            == BehaviorReference.MessageDisplayBehavior)
        {
            var message = behaviorEvent.ViewModel as MessageViewModel;

            if (message != null)
            {
                this.RadToolTipMessage.Text = message.Message;
                this.RadToolTipMessage.Title = message.Tittle;
                this.RadToolTipMessage.Visible = true;
                this.RadToolTipMessage.Animation = 
                                        ToolTipAnimation.Fade;
                   
                this.RadToolTipMessage.Show();
            }
        }
    }
    
    //..I am using Telerik RadToolTip to display messages
    //..for more information how to use RadToolTip visit: 
    // http://www.telerik.com/products/aspnet-ajax/tooltip.aspx
    protected override void CreateChildControls()
    { 
        this.Controls.Add(this.RadToolTipMessage);

        base.CreateChildControls();
        base.EnsureChildControls();
    }
    protected override void OnInit(EventArgs e)
    {
        this.RadToolTipMessage = new RadToolTip()
        {
            ID = "RadToolTipMessage",
            Skin = "Metro",
            VisibleOnPageLoad = true,
            RelativeTo = 
                ToolTipRelativeDisplay.BrowserWindow,
            Visible = false,
            Position = ToolTipPosition.Center,
            HideEvent = ToolTipHideEvent.ManualClose,
            ShowDelay = 15000,
            AutoCloseDelay = 10000,
            HideDelay = 15000
        };

        base.OnInit(e);
    }
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>