Dynamically fill in combo with MVC 5

Asked

Viewed 452 times

1

In my file .cshtml, I got these divs:

<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 these guys up like this: Day => from 1 to 31 Month => from 1 to 12 Year => from 1900 to Present

Usage MVC 5, Jquery and VS 2013

  • Ué, lost formatting. I sent a snippet of code from my cshtml and lost. I will edit and try to recover formatting as it is easier to read.

2 answers

1

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

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

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

In 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>

Thus, you will 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

0

I solved it like this: Example for Day, but it goes for the 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.