Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - How to use Required message inside a EditorTemplates

I have a simple model:

    public abstract class Person
    {
        [Required(ErrorMessage = "Il nome è obbligatorio!!!")]
        [UIHint("txtGeneric")]
        [Display(Name = "Nome")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Il cognome è obbligatorio!!!")]
        [UIHint("txtGeneric")]
        [Display(Name = "Cognome")]
        public string Surname { get; set; }

        [Required(ErrorMessage = "L'email è obbligatoria e deve essere in formato valido ([email protected])!!!")]
        [UIHint("txtEmail")]
        [Display(Name = "E-mail")]
        public string Email { get; set; }
}

I created inside the EditorTemplate the txtGeneric.cshtml file. It is like this:

@model string
<input name="@ViewData.TemplateInfo.HtmlFieldPrefix" id="@ViewData.TemplateInfo.HtmlFieldPrefix" data-validation="required" data-validation-error-msg="MESSAGE_TO_PUT" value="@Model" />

I want to know how to take the text associated Errormessage of Required attribute to put into my txtGeneric.cshtml file. How can I do that?

Thanks

like image 519
pinguinone Avatar asked Jul 04 '14 22:07

pinguinone


People also ask

How do you make a field required in Cshtml?

cshtml. Add the "[Required]" attribute to the field that you want to make mandatory for insertion. The required attribute requires the "System. ComponentModel.

How can send error message from Controller view in ASP.NET MVC?

To pass error to the view we can use ModelState. AddModelError method (if the error is Model field specific) or simply ViewBag or ViewData can also be used.

How can use client side validation in MVC?

We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level. For client-side validation, the values of above both the keys must be true.

Which namespaces are required to data annotation using MVC?

ComponentModel. DataAnnotations namespace that you can use to apply to (decorate) classes or properties to enforce pre-defined validation rules.


1 Answers

This works:

 @{
     string requiredMsg = "";
     foreach (var attr in @Html.GetUnobtrusiveValidationAttributes(@ViewData.TemplateInfo.HtmlFieldPrefix, @ViewData.ModelMetadata))
     {
         if (attr.Key == "data-val-required") {
             requiredMsg = attr.Value.ToString();
         }
     }
 }

or this:

 @{
     string requiredMsg = "";
     IEnumerable<ModelClientValidationRule> clientRules = ModelValidatorProviders.Providers.GetValidators(ViewData.ModelMetadata, ViewContext).SelectMany(v => v.GetClientValidationRules());
     foreach (ModelClientValidationRule rule in clientRules)
     {
         if (rule.ValidationType == "required")
         {
             requiredMsg = rule.ErrorMessage;
         }
     }
 }
like image 96
zety Avatar answered Sep 27 '22 01:09

zety