0

I have View:

@model MvcApplication2.Models.HomeModel

@{
    ViewBag.Title = "Home";
}

<h2>Home</h2>
<a>Witaj</a>
@Model.UserName

@using (Html.BeginForm())
{

    <a>Podaj hasło</a>
    @Html.PasswordFor(m => m.Password)
    @Html.ValidationMessageFor(x => x.Password)

    <input type="submit" />
}

and Controller

    using System.Web.Mvc;
using System.Web.UI;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomeModel model = new HomeModel() { UserName = "John" };
            return View(model);
        }

        public JsonResult CheckPassword(string password)
        {
            bool result = false;
            if (password.Length < 4)
            {
                result = false;
            }
            else
            {
                result = true;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}

and Model:

  using System.Web.Mvc;

namespace MvcApplication2.Models
{
    public class HomeModel
    {
        public string UserName { get; set; }

        [Remote("CheckPassword", "Home", ErrorMessage = "Wpisz lepsze hasło")]
        public string Password { get; set; }
    }
}

When should remote validation method fire? When I click input button? What I do wrong?

netmajor
  • 6,507
  • 14
  • 68
  • 100
  • Solution find [here][1] - need setup in web.config [1]: http://stackoverflow.com/questions/19680967/remote-attribue-validation-not-firing-in-asp-net-mvc – netmajor Mar 08 '14 at 20:46

2 Answers2

2

Are you missing unobtrusive jQuery references in your view / Razor Layout?

<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
Martin4ndersen
  • 2,806
  • 1
  • 23
  • 32
1

In CheckPassword method password variable is not matching with the Model Password. It should be in TitleCase :- CheckPassword(string Password)

Prabhat
  • 65
  • 4