1

Here is what my code look like rendered as HTML

<input data-val="true" data-val-remote="User name already exists. Please enter a different user name." data-val-remote-additionalfields="*.Name" data-val-remote-url="/OrchardLocal/Genealex.CRM.Core/Home/doesNameExist" data-val-required="Enter Name" id="typeName" name="TypeType.Name" type="text" value="">

Here is the property in my model

[Remote("doesNameExist", "Home", ErrorMessage = "User name already exists. Please enter a different user name.")]
public string Name { get; set; }

The action in my controller:

[HttpPost]
public JsonResult doesNameExist(string Name)
{
    var user = _contactBll.DoesTypeExist(Name);
    if (user)
    {
        return Json("Username is already exist", JsonRequestBehavior.AllowGet);
    }

    return Json(true, JsonRequestBehavior.AllowGet);
}

And in my View I have :

@Html.TextBoxFor(model => model.TypeType.Name, new { id = "typeName" })

I have also included scripts like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>

Yet, when I leave the focus from the textbox, after I have written something in the input field the validation is not fired.

What am I doing wrong?

2
  • make sure that your action is reachable under that url /OrchardLocal/Genealex.CRM.Core/Home/doesNameExist - it looks oddly, moreover we do not have your routing defintion Commented Jun 13, 2015 at 15:40
  • Thank you for the comment, I have found my mistake. Commented Jun 14, 2015 at 12:28

1 Answer 1

1

My mistake was the following :

[HttpPost]
public JsonResult doesNameExist(string Name)
{
    var user = _contactBll.DoesTypeExist(Name);
    if (user)
    {
        return Json("Username is already exist", JsonRequestBehavior.AllowGet);
    }

    return Json(true, JsonRequestBehavior.AllowGet);
}

Instead of [HttpPost], I needed the [HttpGet] over my controller. Switched it up and works just fine.

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

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.