0

This is my Model :

    [Required(ErrorMessage = "Email required!")]
    [Remote("EmailExists","User",ErrorMessage = "Email already")]
    public virtual string Email { get; set; }

View :

    @Html.TextBoxFor(x => x.Email)
    @Html.ValidationMessageFor(x => x.Email)

Controller:

    public ActionResult EmailExists(string Email)
    {
         return Json(!Email.Equals("[email protected]"), 
                                      JsonRequestBehavior.AllowGet);
    }

jquery.validate.min.js and jquery.validate.unobtrusive.min.js are added. And web.config is configured as well.

When I type on Email input it fires EmailExists fine. Returns true/false as well. But it nevers shows the ErrorMessage

And I get this error :

 Erro: uncaught exception: 
 [Exception... "Cannot modify properties of a WrappedNative"
 nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)"  
 location: "JS frame :: chrome://global/content/bindings/autocomplete.xml ::
 onxblpopuphiding :: line 848"  data: no]

Any idea?

2 Answers 2

3

There is nothing in your description that supposes a problem. I've created a new ASP.NET MVC 3 application using the default template, added the model:

public class MyViewModel
{
    [Required(ErrorMessage = "Email required!")]
    [Remote("EmailExists", "Home", ErrorMessage = "Email already")]
    public string Email { get; set; }
}

updated the HomeController:

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

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

    public ActionResult EmailExists(string Email)
    {
        return Json(
            !Email.Equals("[email protected]"), 
            JsonRequestBehavior.AllowGet
        );
    }
}

and the ~/Views/Home/Index.cshtml view:

@model AppName.Models.MyViewModel
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.Email)
    @Html.TextBoxFor(x => x.Email)
    @Html.ValidationMessageFor(x => x.Email)
    <input type="submit" value="OK" />
}

Validation fires fine and correct error messages are shown (tested with Chrome 10.0, IE9 and FireFox 4.0). So the question now is how does your scenario differs than this one?

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

1 Comment

Thanks Darin... I found the problem... It was jquery.validate version... I was using jquery 1.5.1 and jquery.validate 1.7... So I updated to 1.8 and all worked fine...
0

You just need to do this:

    [Required(ErrorMessage = "Email required!")]
    [Remote("EmailExists","User")]
    public virtual string Email { get; set; }

and

    public JsonResult EmailExists(string Email)
    {
        string errorMessage = "Email already";
        if (!Email.Equals("[email protected]"))
            return Json(true, JsonRequestBehavior.AllowGet);

        return Json(errorMessage, JsonRequestBehavior.AllowGet);
    }

Comments

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.