Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default "[field value] already taken" error message in ASP.Net MVC 5?

In ASP.Net MVC 5 View we have an automatic error message popped-up when a control/input in the View tries to create column value in the database (or a field value in a class) which is supposed to be unique but we violate it - such as when creating new user account's email with an already existing email as shown below.

enter image description here

Now, I am supposed to change the error message above into a local language in my project:

... is already taken <-> ... [is already taken in local language]

How to change the "[field value] already taken" (automatic) error message?

According to this post:

Issue with username validation message in asp.net identity

We could just try to search for "Name {0} is already taken" in the whole solution, but when I did that, I couldn't find it.

enter image description here

Was that a valid solution in earlier MVC but outdated for MVC 5?

(Note: unfortunately, the post does not indicate which ASP.Net MVC version the OP is working with)

like image 453
Ian Avatar asked Aug 10 '16 07:08

Ian


1 Answers

I have implemented this via using Remote in System.ComponentModel.DataAnnotations. Please refer following code :

In ViewModel (Properties) :

        [Display(Name = "Email")]
        [Remote("IsValidUserName", "Remote", AdditionalFields = "Email", HttpMethod = "Post")]
        public string Email { get; set; }

Here "Remote" is a Controller Name and "IsValidUserName" our Method. Like below:

public class RemoteController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult IsValidUserName()
        {
            string email= Request.Form["Email"];  
            string message = "Email '"+ email+ "' is already taken";
            if (!string.IsNullOrEmpty(email))
            {
                using (DbContext db = new DbContext())
                {
                    if (!db.tb_Users.Any(x => x.email== email))
                    {
                        message = string.Empty;
                    }
                }
            }
            if (message == string.Empty)
                return Json(true, JsonRequestBehavior.AllowGet);
            else
                return Json(message, JsonRequestBehavior.AllowGet);
        }
}

and on View page you have to use this like below:

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

Output will be : enter image description here

Points to remember: You have to use following JavaScripts for running this:

<script   src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script   src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.jss"></script>
<script   src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>

Hope it helps you.

Thanks.

like image 130
Sunil Kumar Avatar answered Sep 23 '22 14:09

Sunil Kumar