locked
Show ErrorMessageResourceName = "Required" on mi view page RRS feed

  • Question

  • User1991482975 posted

    Hi, in my model i have this field:

    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
            [Display(Name = "PartnerTitle", ResourceType = typeof(ViewRes.NamesPartner))]
            [StringLength(2000)]
            [AllowHtml]
            public string PartnerTitle { get; set; }

    and i want to show on my view only the required error for the PartnerTitle field  in a div,

    ErrorMessageResourceName = "Required",



    how to achive this?

    Wednesday, December 2, 2020 10:38 AM

Answers

  • User1686398519 posted

    Hi grafic.web, 

    Do you mean you don't want to use html helper to render the page?Do you want to get the value in the resource type file directly on the page?You can refer to the code below.

    To be honest, by using HTML Helpers, you can reduce the tedious typing of HTML tags that must be performed to create a standard HTML page.

    <input class="form-control" data-val="true"
           data-val-length="@ViewRes.ValidationStringsLength"
           data-val-length-max="2"
           data-val-required="@ViewRes.ValidationStrings"
           id="PartnerAdvertiseTitle"
           name="PartnerTitle"
           type="text"
           value="">
    <span data-valmsg-for="PartnerTitle" data-valmsg-replace="true"></span>

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2020 8:08 AM

All replies

  • User1686398519 posted

    Hi grafic.web,

    The ResourceType property corresponds to the type of resource file (your file is ViewRes), and the name property ccorresponds with the name of the string in the resource file which you wish to bind the property to.

    I wrote an example based on the code you provided. You can refer to it.

    Model

    public class TestThreeModel
    {
       [Required(ErrorMessageResourceName = "ValidationStrings", ErrorMessageResourceType = typeof(ViewRes))]
       [Display(Name = "NamesPartner", ResourceType = typeof(ViewRes))]
       [StringLength(2000)]
       [AllowHtml]
       public string PartnerTitle { get; set; }
    }

    Controller

    public ActionResult Index()
    {
       return View();
    }
    [HttpPost]
    public ActionResult Index(TestThreeModel model)
    {
       ViewBag.Message = "success";
       return View();
    }

    View

    @{
        ViewBag.Title = "Index";
        Layout = null;
    }
    <h2>Index</h2>
    @model DailyMVCDemo.Models.TestThreeModel
    @ViewBag.Message
    @using (Html.BeginForm("Index", "TestThree", FormMethod.Post))
    {
        @Html.DisplayNameFor(m => m.PartnerTitle)
        @Html.TextBoxFor(m => m.PartnerTitle)
        @Html.ValidationMessageFor(m => m.PartnerTitle)
        <button type="submit">submit</button>
    }
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    Here is the result. 

    Best Regards,

    YihuiSun

    Thursday, December 3, 2020 3:11 AM
  • User1991482975 posted

    Hi,

    in fact i am writing the code in html like this :

    <input class="form-control" data-val="true" data-val-length="The field Titre de l'annonce must be a string with a maximum length of 2000." data-val-length-max="2000" data-val-required="Champ Titre de l'annonce est obligatoire" id="PartnerAdvertiseTitle" name="PartnerAdvertiseTitle" type="text" value="">

    and i only need to get this part from my model :

    data-val-length="The field Titre de l'annonce must be a string with a maximum length of 2000." 

    and

    data-val-length-max="2000" data-val-required="Champ Titre de l'annonce est obligatoire"



    how to do?

    Thursday, December 3, 2020 8:31 AM
  • User1686398519 posted

    Hi grafic.web, 

    Do you mean you don't want to use html helper to render the page?Do you want to get the value in the resource type file directly on the page?You can refer to the code below.

    To be honest, by using HTML Helpers, you can reduce the tedious typing of HTML tags that must be performed to create a standard HTML page.

    <input class="form-control" data-val="true"
           data-val-length="@ViewRes.ValidationStringsLength"
           data-val-length-max="2"
           data-val-required="@ViewRes.ValidationStrings"
           id="PartnerAdvertiseTitle"
           name="PartnerTitle"
           type="text"
           value="">
    <span data-valmsg-for="PartnerTitle" data-valmsg-replace="true"></span>

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2020 8:08 AM