Saturday 10 January 2015

MVC4 Razor: How to compare password and confirm password in ASP.NET MVC4 Razor

Hi friends, in this article I will explain about How to create a DropDownList field for MVC4 Razor view?

Compare.cshtml View

@model MvcNew.Models.Compare

@{
    ViewBag.Title = "Compare";
}

<h2>Compare</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Compare</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.c_pwd)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.c_pwd)
            @Html.ValidationMessageFor(model => model.c_pwd)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



Campare.cs Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using CompareAttribute = System.Web.Mvc.CompareAttribute;

namespace MvcNew.Models
{
    public class Compare
    {
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Password required")]
        public string Password { get; set; }

        [Display(Name = "Confirm new password")]
        [Required(ErrorMessage = "Enter Confirm Password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        [DataType(DataType.Password)]
        public string c_pwd { get; set; }
    }
}

HomeController.cs Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcNew.Models;

namespace MvcNew.Controllers
{
    public class HomeController : Controller
    {
         public ActionResult Compare()
        {
            return View();
        }
    }
}


The output of the above code as shown in the below figure.

4 comments:

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.