0

My Select List Data : List of Strings

public static class QuestionsSecretes
    {
        public static readonly IList<String> Liste_Questions_Secretes = new List<String>() {
                "Selectionner Une Question",
                "Quelle était la couleur de votre première voiture ?",
                "Quel est le nom de votre école primaire ?",
                "Quel est le nom de votre premier animal de compagnie ?",
                "Quel est votre plat préféré ?",
                "Quelle était la marque de votre première voiture ?" ,
                "Quel est le nom de jeune fille de votre mère ?",
                "Quel est le nom de votre commune de naissance ?",
            };

    }

ViewModel : annotation attribute

 [Required(ErrorMessage = "Veuillez Selectionner votre Question Secrete ")]
        public string QuestionSecrete { get; set; }

i also tried this one :

 [StringLength(Int32.MaxValue, MinimumLength = 5, ErrorMessage = "Veuillez Selectionner votre Question Secrete")]

*My View : i set value of the first element to string.empty in order to fire the validaion , bu no chance *

 <div class="inputs-block">
                                                 <div class="col-md-12 col-sm-12">
                                                    <div class="col-md-2 col-sm-2 marg8">Question Secrete : </div> 
                                                    <div class="col-md-8 col-sm-8" style="padding-left: 10px; padding-right: 10px;">
                                                        <label class="selct">
                                                        <select name="QuestionSecrete" id="QuestionSecrete">
                                                            <% foreach (var item in QuestionsSecretes.Liste_Questions_Secretes)
                                                               {%>
                                                             <%if (item.Equals(QuestionsSecretes.Liste_Questions_Secretes[0]))
                                                                      {%>
                                                                        <option value="<%: String.Empty %>"><%: item %></option>
                                                            <%}else if (!String.IsNullOrEmpty(Model.QuestionSecrete) && Model.QuestionSecrete.Equals(item))
                                                                      {%>
                                                                        <option value="<%: item %>" selected="selected"><%: item %></option>
                                                            <%}else{ %>
                                                                   <option value="<%: item %>"><%: item %></option>
                                                               <%} %>
                                                            <%} %>
                                                        </select>
                                                        </label>
                                                       <div class="text-danger">
                                                            <%: Html.ValidationMessageFor(m => m.QuestionSecrete)%>
                                                         </div>
                                                    </div>



                                                </div>

i can't get the validation Message on select to work. thnaks for your valuable time

1
  • You need to use the html helpers - @Html.DropDownListFor() or manually add all the appropriate data-val attributes used by client side validation. Commented Oct 27, 2015 at 21:24

1 Answer 1

1

Not sure why you are defining select manually. You can use the Html.DropDownListFor extension method to generate the select control.

<%: Html.DropDownListFor(model => model.QuestionSecrete, new  System.Web.Mvc.SelectList(Model.Liste_Questions_Secretes ), string.Empty, new {@class="form-control"})%>
<%: Html.ValidationMessageFor(model => model.QuestionSecrete)%>

Using above extension, rendered html shows a select control with a blank item. Also, validation will be fired if user does not choose any item in select while submitting a form.

The Model property is defined as

[Required(ErrorMessage = "Veuillez Selectionner votre Question Secrete ")]
public string QuestionSecrete { get; set; }

I have created this dotnet fiddle to demonstrate your example. You can run the fiddle to see the validation behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @user1672994 using the helper solved the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.