简体   繁体   English

ASP.NET MVC:所选值不适用于列表<selectlistitem></selectlistitem>

[英]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.

相关问题 具有下拉列表的Asp.Net MVC和SelectListItem协助 - Asp.Net MVC with Drop Down List, and SelectListItem Assistance 在 ASP.NET Core MVC 中如何插入返回 List 的方法的返回值<selectlistitem>在 Session</selectlistitem> - In ASP.NET Core MVC how insert the return value of a method that returns List<SelectListItem> in a Session asp.net mvc 5 SelectListItem有条件 - asp.net mvc 5 SelectListItem with condition 获取asp.net mvc中所选复选框值的列表 - get list of selected checkbox value in asp.net mvc 将通用 IDictionary 转换为 ASP.NET MVC IEnumerable<selectlistitem> : 选择 Selected 项目</selectlistitem> - Converting an generic IDictionary to an ASP.NET MVC IEnumerable<SelectListItem>: choosing the Selected item ASP.NET selectlistitem值int - ASP.NET selectlistitem value int 在 ASP.NET Core 中提交表单后如何保持 SelectListItem 值被选中? - How to keep SelectListItem value selected after submitting form in ASP.NET Core? ASP.NET Mvc获取列表的最佳方法 <SelectListItem> 从EntityContext吗? - ASP.NET Mvc Best Way to get List<SelectListItem> From EntityContext? ASP.NET 中的 MVC - 使用 IEnumerable 显示值而不是 Id<selectlistitem></selectlistitem> - MVC in ASP.NET - Displaying a value instead of Id using IEnumerable<SelectListItem> IEnumerable <SelectListItem> ASP.NET MVC 5中的简单成员资格错误 - IEnumerable<SelectListItem> error in Simple Membership in ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM