Paul
Paul

Reputation: 12799

asp.net mvc 3 Remote validation problem

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?

Upvotes: 0

Views: 1923

Answers (2)

Jalali Shakib
Jalali Shakib

Reputation: 626

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);
    }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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?

Upvotes: 3

Related Questions