Tuesday 27 December 2016

Asp.Net MVC Validation Compare Passwords Example

Once we add new model open it and write code like as shown below.


using System.ComponentModel.DataAnnotations;

namespace MVCExamples.Models
{
public class UsersModel
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
[Compare("Password",ErrorMessage= "Both Password and Confirm Password Must be Same" )]
public string ConfirmPassword { get; set; }
public string Location { get; set; }
}
}


Now open new controller and write the code like as shown below


using System.Web.Mvc;

namespace MVCExamples.Controllers
{
public class UserController : Controller
{
// GET: User
public ActionResult Index()
{
return View();
}
public ActionResult UserRegistration()
{
return View();
}
}
}

Now right click on UserRegistration method and select Add View

Once click Add View new template will open in that select Template type “Create” and Model class as our “UsersModel” and click Add like as shown below.


No comments:

Post a Comment