0

i'am using class library for my Models and create my controllers in Area now i want to use remote validation but it's not working,actually it's not rendered.

my model in Models class library

[System.ComponentModel.DataAnnotations.RegularExpression(@"(?=.*[a-zA-Z0-9]).{4,20}$", ErrorMessage = "error")]
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "error")]
[System.Web.Mvc.Remote("UserNameExist","AdminUser","Admin",ErrorMessage="error")]
public string UserName { get; set; }

my controller in Admin Area

[HttpPost]
public JsonResult UserNameExist(string UserName)
{
    using (DataBaseContext dbContext = new DataBaseContext())
    {
        var user = dbContext.Users.SingleOrDefault(current => current.UserName == UserName);
        return Json(user == null);
    }
}

also i was added jquery and jquery.validation and jquery.validate.unobtrusive and i was added this to my webconfig

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

Updated

rendered html is :

<input class="form-control directionEn valid" data-val="true" data-val-regex="error" data-val-regex-pattern="(?=.*[a-zA-Z0-9]).{4,20}$" data-val-required="error" id="UserName" name="UserName" placeholder="User Name" type="text" value="">

there is not attribute for remote validation

User View:

@model Models.User
@section Scripts
{
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}
<div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3">
        <div class="form-group">
            <a class="btn btn-success" href="/Admin/AdminUser/List">List</a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Add New User</h4>
            </div>
            <div class="panel-body">
                @using (Html.BeginForm("Add", "AdminUser", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.Raw(System.Web.HttpUtility.HtmlDecode((Html.ValidationSummary(true, "", htmlAttributes: new { @class = "alert alert-danger" }) ?? (object)"").ToString()))

                    <div class="form-group">
                        <label>
                            UserName
                        </label>
                        @Html.TextBoxFor(model => model.UserName, htmlAttributes: new { @class = "form-control directionEn", @placeholder = "User Name" })
                        @Html.ValidationMessageFor(model => model.UserName, "", htmlAttributes: new { @class = "validationError" })
                    </div>

                }
            </div>
        </div>
    </div>
</div>

what's wrong in my code ???

barzin.A
  • 1,554
  • 2
  • 12
  • 20
  • What is being rendered? (show the actual html being generated). And you should be using `[Remote("UserNameExist", "AdminUser", "Admin", ErrorMessage="error")]` (refer [overloads](https://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute.remoteattribute(v=vs.118).aspx#M:System.Web.Mvc.RemoteAttribute.)) –  Feb 02 '17 at 08:34
  • There is nothing wrong with the code you have shown (it generates the correct `data-val-remote-*` attributes for me). Something you have not shown us it causing the issue. –  Feb 02 '17 at 09:25
  • I think using Area or Class library for Controllers and Models make it wrong, i don't know how to fix it – barzin.A Feb 02 '17 at 10:24
  • Show me your View as well – Ubiquitous Developers Feb 02 '17 at 13:01

1 Answers1

0

First of all Remove HttpPost from your Controller. I don't think RemoteValidation Works on HttpPost

public JsonResult UserNameExist(string UserName)
{
    using (DataBaseContext dbContext = new DataBaseContext())
    {
        var user = dbContext.Users.SingleOrDefault(current => current.UserName == UserName);
        return Json(user == null);
    }
}

Second In your model I don't think there is the use of Admin Parameter

[System.ComponentModel.DataAnnotations.RegularExpression(@"(?=.*[a-zA-Z0-9]).{4,20}$", ErrorMessage = "error")]
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "error")]
[System.Web.Mvc.Remote("UserNameExist","AdminUser",,ErrorMessage="error")]
public string UserName { get; set; }

Also I consider you have display validation message

@Html.TextBoxFor(i => i.UserName)
@Html.ValidationMessageFor(i => i.UserName)

You can also put debbuger on UserNameExist function and check that function is called or not.

My Working Fiddler : https://dotnetfiddle.net/dlME8p

Ubiquitous Developers
  • 3,637
  • 6
  • 33
  • 78