[英]ASP.NET MVC : selected value not working on a List<SelectListItem>
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我将它保存在
Viewbag
中,这样我就可以像这样将它传递给视图
[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.现在,问题是在我的 controller 方法的
[HttpGet]
上不加载所选值,但在[HttpPost]
中它加载。
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.
但正如我在使用 Enero 加载它时所说的那样。 the first value of the list despite having another selected.
列表的第一个值,尽管有另一个选择。
Any clues?有什么线索吗?
I solve it using Session variable instead of viewbag.我使用 Session 变量而不是 viewbag 来解决它。 I change this
我改变这个
ViewBag.mes = new SelectList(Fecha.Meses(), "Value", "Text", mes);
for this为了这
Session["mes"] = new SelectList(Fecha.Meses(), "Value", "Text", mes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.