If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Form validation with ASP.NET MVC using IDataErrorInfo Interface

by Farooq Kaiser 13. August 2009 01:36

As I already shown in my previous article Form validation with ASP.NET MVC. In this article, I will explore an alternative method of implementing validation logic. I will show you how to perform validation by using the IDataErrorInfo interface.

public interface IDataErrorInfo
{
    // Properties
    string Error { get; }
    string this[string columnName] { get; }
}

We will modify last article that will implement IDataErrorInfo Interface. To implement the IDataErrorInfo interface, we must create a partial class. Our tblComment Partial class is shown below.

public partial class tblComment : IDataErrorInfo
    {
        private Dictionary<string, string> _errors = new Dictionary<string, string>();
        partial void OnNameChanging(string value)
        {
            if (string.IsNullOrEmpty(value.Trim()))
                _errors.Add("Name", "Name is required.");
        }
        partial void OnEmailChanging(string value)
        {
            if (string.IsNullOrEmpty(value.Trim()))
                _errors.Add("Email", "Email is required.");
        }
        partial void OnMessageChanging(string value)
        {
            if (string.IsNullOrEmpty(value.Trim()))
                _errors.Add("Message", "Message is required.");
        }
        #region IDataErrorInfo Members
        public string Error
        {
            get { return string.Empty; }
        }
        public string this[string columnName]
        {
            get
            {
                if (_errors.ContainsKey(columnName))
                    return _errors[columnName];
                return string.Empty;
            }
        }
        #endregion
    }

One thing to note, when the Entity Framework generates an entity class, the Entity Framework adds partial methods to the class automatically. The Entity Framework generates OnChanging and OnChanged partial methods that correspond to each property of the class. We will modify our UserCommentController class as shown below.

public class UserCommentController : Controller
    {
        private CommentEntities _db = new CommentEntities();
        [AcceptVerbs("GET")]
        public ActionResult UserComment()
        {
            return View(new tblComment());
        }
        [AcceptVerbs("POST")]
        public ActionResult UserComment([Bind(Exclude = "CommentID")] tblComment commentToCreate)
        {
            // Validate
            if (!ModelState.IsValid)
                return View();
            // Add to database
            try
            {
                _db.AddTotblComment(commentToCreate);
                _db.SaveChanges();
                return RedirectToAction("UserComment");
            }
            catch
            {
                return View();
            }
        }
    }

The ASP.NET MVC framework creates the instance of the tblComment passed to the UserComment () action by using a model binder. The model binder is responsible for creating an instance of the tblComment object by binding the HTML form fields to an instance of the tblComment object. Now I will run the project and it will display the usercomment view as shown below.

Summary 

In this article, we explored Form validation with ASP.NET MVC using IDataErrorInfo Interface.

.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.NET | asp.net | C#

Comments

Jobs Autos Real estate Videos Power by Google