Fill combo dynamically with MVC 5

1

In my .cshtml file, I have these DIV's:

<div class="grid_14">
    <select id="txtDia" name="txtDia" class="grid_2" required>
        <option>Dia</option>
    </select>
    <select id="txtMes" name="txtMes" class="grid_2" required>
        <option>Mês</option>
    </select>
    <select id="txtAno" name="txtAno" class="grid_2" required>
        <option>Ano</option>
    </select>
</div>

How do I fill in these guys like this: Day = > from 1 to 31 Month = > from 1 to 12 Year = > from 1900 to Current

Using MVC 5, JQuery and VS 2013

    
asked by anonymous 17.03.2014 / 15:12

2 answers

1

You will have some work if you want to work this way, because you have months that are less than 31 days, and leap years that change those days too.

What you can use is in your model, in the field of the date, put the DataAnnotation:

[DataType(DateType.Date)]
Public DateTime DataVencimento{get;set;}

In the View you use:

<div class="form-group">                       
        @Html.LabelFor(model => model.DataVencimento, new { @class = "col-xs-4" })
        <div class="col-xs-6">
            @Html.EditorFor(model => model.DataVencimento)
            @Html.ValidationMessageFor(model => model.DataVencimento)
        </div>
 </div>

So, you'll have all this part of date validation ready, using DatePicker. DatePicker http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-03-74-metablogapi/7737.image_5F00_477473C0.png

    
17.03.2014 / 15:30
0

I solved it like this: Example for Day, but good for others.

<div class="grid_14">
    <select id="txtDia" name="txtDia" class="grid_2" required>
        <option>Dia</option>
            @{
                for(int i = 1; i < 32; i++)
                {
                    @:<option>@i</option>
                }
             }
    </select>
</div>
    
17.03.2014 / 15:25