- Model binding allows you to map and bind the HTTP request data with a model.
- Model binding implicitly goes to work when an action method has parameter.
- Model binding can be explicitly invoked using UpdateModel and TryUpdateModel method.
In this article we will discuss if not implicitly how we can trigger Model binding explicitly.
Why Model binding explicitly?
Sometimes there are cases where we need to trigger the model binding process explicitly. The model binding fills the model class with the form values and also outputs ModelState as by-product, that means if the model class has Data Annotation attributes applied for validation, the model binding validates the model properties against Data Annotation attributes and updates ModelState accordingly. If the validation result is success then the ModelState is true else Vice Versa.
So, in short we know we can trigger explicit model binding for filing model object and also to validate model values if Data Annotation attributes are used.
How Model binding explicitly?
MVC provides two methods which accomplished the task of model binding.
- UpdateModel
- TryUpdateModel
Both the methods perform same task of explicit model binding, only difference is that the UpdateModel method throws exception if ModelState is not valid and TryUpdateModel not.
How to use these methods?
Lets have a model first. We will use a simple Register model class.
Model:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace SampleRegisterForm.ViewModel { public class Register { [Required(ErrorMessage="FirstName is Mandatory !!")] public string FirstName { get; set; } [Required(ErrorMessage = "LastName is Mandatory !!")] public string LastName { get; set; } [Required(ErrorMessage = "Email is Mandatory !!")] public string Email { get; set; } [Required(ErrorMessage = "Password is Mandatory !!")] public string Password { get; set; } [Required(ErrorMessage = "Confirm Password is Mandatory !!")] [Compare("Password")] public string ConfirmPassword { get; set; } } }
The above is our model. We have referred this model class in our View. We will rendering controls for the properties of model inside a form. On submit the form will be posted to controller and there we will perform explicit model binding.
View:
@model SampleRegisterForm.ViewModel.Register @{ ViewBag.Title = "Register"; } <h2>Register</h2> @using (Html.BeginForm()) { @Html.EditorForModel("Register") <br /> <br /> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /> }In the view, we have referred Register model and rendered a form using BeginForm helper. We have rendered controls for model properties using EditorForModel helper. We have two buttons i.e. submit and reset.
UpdateForModel:
[HttpPost] public ActionResult Register(FormCollection collection) { SampleRegisterForm.ViewModel.Register register = new SampleRegisterForm.ViewModel.Register(); try { UpdateModel(register); } catch (Exception e) { return View(register); } return View(); }We have wrapped UpdateModel method inside a try block. The UpdateModel throws exception if model state is not valid. When the model state is not valid, the UpdateModel throws exception and in catch block we are returning model with errors to the view.
TryUpdateModel:
[HttpPost] public ActionResult Register(FormCollection collection) { SampleRegisterForm.ViewModel.Register register = new SampleRegisterForm.ViewModel.Register(); if (TryUpdateModel(register)) { return View(); } else { return View(register); } }The TryUpdateModel method returns true or false based on the ModelState.
So, this is how you can use UpdateModel and TryUpdateModel for explicit model binding.
No comments:
Post a Comment