asp.net · ebook

[ASP.NET] TryUpdateModel and FormValueProvider

1. TryUpdateModel

TryUpdateModel method: perform a process called model binding where data value are used from the browser request to populate property of our data object. FormValueProvider class will provides value from form data.

Example:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) return;
 
            var rsvp = new GuestResponse();
 
            if (!TryUpdateModel(rsvp, new FormValueProvider(ModelBindingExecutionContext))) return;
 
            ResponseRepository.GetRepository().AddResponse(rsvp);
 
            if (rsvp.WillAttend.HasValue && rsvp.WillAttend.Value)
                Response.Redirect("seeyouthere.html");
            else
                Response.Redirect("sorryyoucantcome.html");
        }

Source: ASP.NET 4.5 Pro Apress – Chapter 1

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s