0

i am using bootstrap multiselect plugin

            <select class="btn btn-default  col-md-1" name="fw_dropdown" id="fw_dropdown" multiple="multiple" required="required">
            @foreach (var fw in Model.FWIndex)
            {
                <option value="@fw">@fw</option>
            }
        </select>

after I add the required attribute, the form is able to display a message that the field is not selected. However, when I proceed to select the field, the message kept showing, which seems that the selected value cannot be received. When I reload the project and select the required field, I am able to proceed with the submit query.

How do I fix this?

  • Adding a `[Required]` attribute to a collection property only means that it cannot be `null` (an empty collection with no elements passes). And using a repository pattern has nothing to do with it. You still have a model with properties, but in any case, if you editing data, thenyou should be using a view model - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). –  Jun 08 '16 at 01:52
  • @StephenMuecke I have edited the question. can you help take a look at it? Thanks! – yellowbowlmigration Jun 08 '16 at 03:53
  • That's not how to generate a ` –  Jun 08 '16 at 03:57

1 Answers1

1

Seems that the required attribute only works for select class in IE when multiple selection is not enabled (I'm using bootstrap multiselect).

Instead I write a function in the submit button to achieve validation.

<script>
        $(document).ready(function(event){
            $("form").submit(function(event){
                var fw=$("#fw_dropdown").val();
                if(fw=="" || fw==null){
                    alert("Please select FW!");
                    //prevent form submission
                    event.preventDefault();
                }
                //allow form submission only when if is not true
            });
        })
</script>

To generate the form in the web application I used the html helper.

@using (Html.BeginForm("Query", "Actuals"))

That's why "form" is used instead of form id