Well, I think I understand your question...
Assuming you have the number of the month and a ComboBox
empty, we can do this in a few steps :
First : we need to find out how many days are there in the month:
public static IEnumerable<DateTime> GetDiasNoMes(int mes, int? ano = null)
{
ano = ano ?? DateTime.Now.Year;
return Enumerable.Range(1, DateTime.DaysInMonth(ano, mes)) // Dias: 1, 2 ... 31 etc. É um IEnumerable<int>
.Select(dia => new DateTime(ano, mes, dia)) // Mapeia as datas;
}
According to : we need to popular the ComboBox
:
var itens = dataSource.AddRange(GetDiasNoMes(10) //Pega de outubro
.Select(data => new SelectListItem
{
Value = data.Day.ToString(),
Text = data.Day.ToString()
});
comboBox1.Items.Clear(); //Vamos garantir que estaja vazio
comboBox1.Items.AddRange(itens);
Second () : Use jQuery Ajax to update
Controller :
[HttpGet]
public JsonResult Atualizar(int mes)
{
var itens = GetDiasNoMes(mes) //Pega do mes
.Select(data => data.Day);
return Json(itens, JsonRequestBehavior.AllowGet);
}
View :
@Html.DropDownList("Mes", String.Empty)
@Html.DropDownList("Dia", null)
<script type="text/javascript">
$(() => {
$( '#Mes' ).change( function () { //Quando o mês mudar/for selecionado entramos aqui
let valor = $( this ).val(); //pegamos o valor
$.ajax({ //Requisição
url: '@Url.Action("Atualizar","MeuController")',
data: { mes : valor }, //passamos o parametro para a ação
success : ( lista ) => { // caso sucesso
var dias = $( '#Dia' ); //dropdown
dias.empty(); //limpamos as options
lista.each( (index, data) => { //foreach
dias.append( '<option value="' + data + '">' + data + '</option>' ); //adicionamos uma option
});
}
});
})
})
</script>
C# or Javascript?
– Jéf Bueno
It is in c# to present in back office, the client has to select from 1 to 31 the day he wants
– Ricardo Gonçalves
I don’t know what it is back office. It’s to return a
SelectList
for View, this?– Jéf Bueno
Give more details of the requirements, as should be the criterion. Give a context, post the code where it will be used.
– Maniero
Right! the goal is to have a combobox to return a selectList
– Ricardo Gonçalves
@Ricardogonçalves, you already said that, now tell him what he should be like. We can help, we can make the code for you, which is more than normal, but you need to detail how the code needs to be done. We have no way of guessing. You mean to say what should be in the combobox (or dropdown which I think is what you really want and not a combobox) When should each shape appear, based on what? Post something that is doing for us to see where we will fit what is wanting.
– Maniero
I gave an example above, the goal is to create a box where the customer can select day 1, or day 2, or day 3... the box only has to display numbers from 1 to 31 and the customer choose which number he wants.
– Ricardo Gonçalves
What you’ve codified so far?
– Thiago Lunardi
On the HTML side I have a label called "drpDia", and I need to load this label with numbers from 1 to 31. I know you can do this in Javascript, but I don’t know anything about Javascript, so I was going by C#, where I know more or less that I have to make a list from 1 to 31 and then I have to fill the label "drpDia" with these numbers. correct?
– Ricardo Gonçalves
Put all this in the question so people can help you better. It’s about to reopen.
– Maniero
edited the question
– Ricardo Gonçalves
It doesn’t have much to do with it, but i did an introduction to jQuery (which is the easiest way for you to do what you need).
– Leonel Sanches da Silva