2

I am trying to implement remote validation by following tutorial from Here but it is not working in my case My code as follows Web.Conf

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="CrystalImageCleaner-AutoStart" value="true" />
    <add key="CrystalImageCleaner-Sleep" value="60000" />
    <add key="CrystalImageCleaner-Age" value="120000" />
  </appSettings>

Site.Master

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script>
   <script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery.validate.unobtrusive.js")%>"></script>

View

<div class="editor-field">
     <%= Html.TextBoxFor(model => model.CNIC)%>
      <%= Html.ValidationMessageFor(model => model.CNIC)%>
</div>

Controller

public ActionResult CheckDuplicate(string myvar)
        {
            return Json(!myvar.Equals("362-662-1"), JsonRequestBehavior.AllowGet);
        }

Model

[Remote("CheckDuplicate", "Home", "Already Exists")]

In firebug i get the following output which is different from exptected

 <input type="text" value="" name="uname" id="uname" data-val-required="This Field is Required" data-val="true">
while tutorial shows the following for its textbox
<input type="text" value="" name="UserName" id="UserName" data-val-required="The UserName field is required." data-val-remote-url="/Validation/IsUID_Available" data-val-remote-additionalfields="*.UserName" data-val-remote="&amp;#39;UserName&amp;#39; is invalid." data-val-regex-pattern="(\S)+" data-val-regex="White space is not allowed" data-val-length-min="3" data-val-length-max="6" data-val-length="The field UserName must be a string with a minimum length of 3 and a maximum length of 6." data-val="true" class="text-box single-line">
1

1 Answer 1

2

The attribute should look like this:

[Remote("CheckDuplicate", "Home", ErrorMessage = "Already Exists")]

If you use the constructor with 3 string arguments they correspond to action, controller and area.

Model:

public class MyViewModel
{
    [Remote("CheckDuplicate", "Home", ErrorMessage = "Already Exists")]
    public string CNIC { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }

    public ActionResult CheckDuplicate(string cnic)
    {
        return Json(!cnic.Equals("362-662-1"), JsonRequestBehavior.AllowGet);
    }
}

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery.validate.unobtrusive.js")%>"></script>

<% using (Html.BeginForm()) { %>
    <%= Html.TextBoxFor(model => model.CNIC)%>
    <%= Html.ValidationMessageFor(model => model.CNIC)%>    
    <input type="submit" value="OK" />
<% } %>

</asp:Content>

Also notice the name of the action argument passed to CheckDuplicate action: it should match the name of the model property.

Sign up to request clarification or add additional context in comments.

6 Comments

i ve converted my app from mvc 2 to mvc 3 can this be causing some problem ??
@Tassadaque, did you perform the changes I suggested in my answer?
i see only one change "ErrorMessage = " that i made but no luck!
@Tassadaque, are you sure that you properly migrated to MVC 3? Maybe there are still references to the old System.Web.Mvc assembly? Verify that in web.config in the <assemblies> section you have the proper version of this assembly.
Oh! As i upgraded my app from mvc 2 long time ago we write our own remote validation attribute with the same name so that was causing all the trouble Thanks for your help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.