-2

I have a list of month like this

public static class Fecha
{
    public static IEnumerable<SelectListItem> Meses()
    {
        
        List<SelectListItem> meses = new List<SelectListItem>();
        meses.Add(new SelectListItem { Text = "Enero", Value = "1" });
        meses.Add(new SelectListItem { Text = "Febrero", Value = "2" });
        meses.Add(new SelectListItem { Text = "Marzo", Value = "3" });
        meses.Add(new SelectListItem { Text = "Abril", Value = "4" });
        meses.Add(new SelectListItem { Text = "Mayo", Value = "5" });
        meses.Add(new SelectListItem { Text = "Junio", Value = "6" });
        meses.Add(new SelectListItem { Text = "Julio", Value = "7" });
        meses.Add(new SelectListItem { Text = "Agosto", Value = "8" });
        meses.Add(new SelectListItem { Text = "Septiembre", Value = "9" });
        meses.Add(new SelectListItem { Text = "Octubre", Value = "10" });
        meses.Add(new SelectListItem { Text = "Noviembre", Value = "11" });
        meses.Add(new SelectListItem { Text = "Diciembre", Value = "12" });
        return meses;
    }
}

And I save it in a Viewbag so I can pass it to the view like this

 [HttpGet]
public ActionResult Tirilla()
        {
             
            int mes = DateTime.Now.Month;           
            ViewBag.mes = new SelectList(Fecha.Meses(), "Value", "Text", mes);
            Tirillas tirillas = new Tirillas();
            List<Tirillas> view = new List<Tirillas>();
            return View(view);
        }

And in my view a read it like this

<div class="col-xs-10">
                <label>Mes</label>
                <div class="search">
                    <div id="custom-search-input">
                        <div class="input-group">                        

                            @Html.DropDownList("mes", (IEnumerable<SelectListItem>)ViewBag.mes, new { @class = "form-control" })
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-info btn-lg">
                                    <i class="glyphicon glyphicon-search"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                </div>
            </div>

Now, the problem is that on the [HttpGet] of my controller method does not load the selected value, but in the [HttpPost] it does.

I check the code and it goes with the selected values all the time. But as i said in the get it load with Enero. the first value of the list despite having another selected.

Any clues?

1 Answers1

-1

I solve it using Session variable instead of viewbag. I change this

 ViewBag.mes = new SelectList(Fecha.Meses(), "Value", "Text", mes);

for this

 Session["mes"] = new SelectList(Fecha.Meses(), "Value", "Text", mes);