Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation for unique username not working in ASP.NET MVC4
    primarykey
    data
    text
    <p>I trying to validate username for uniqueness but due to some reasons its not working fine. I am doing it in the following way</p> <p><strong>MODEL</strong></p> <pre><code>public class System_User { public int Id { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] [Remote("doesUserNameExist", "System_User", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } public bool IsAdmin { get; set; } } </code></pre> <p><strong>Controller</strong></p> <p>I have added the following code to my System_User Controller</p> <pre><code> [HttpPost] public JsonResult doesUserNameExist(string UserName) { var user = Membership.GetUser(UserName); return Json(user == null); } </code></pre> <p><strong>View</strong> I have the default create view generated by mvc with necessary references to jquery</p> <pre><code>@model WhiteBoard.Models.System_User @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;table class="userTable"&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.FirstName) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.FirstName) @Html.ValidationMessageFor(model =&gt; model.FirstName) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.LastName) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.LastName) @Html.ValidationMessageFor(model =&gt; model.LastName) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.UserName) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.UserName) @Html.ValidationMessageFor(model =&gt; model.UserName) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.Password) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.Password) @Html.ValidationMessageFor(model =&gt; model.Password) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(model =&gt; model.IsAdmin) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.IsAdmin) @Html.ValidationMessageFor(model =&gt; model.IsAdmin) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;input class="btn btn-success" type="submit" value="Create User" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; } &lt;div&gt; &lt;p&gt; @Html.ActionLink("Back to List", "Index") &lt;/p&gt; &lt;/div&gt; @section Scripts { @Scripts.Render("~/bundles/jqueryval") &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; } </code></pre> <p>When I ran the application, it neither threw an exception nor the validation worked. </p> <p>May I know where am I making a mistake</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload