从cookie中设置下拉列表的选定值

时间:2017-01-27 07:31:35

标签: javascript cookies

我的表单中有三个下拉列表,我希望不会丢失用户选择的数据,即使我刷新或在其他页面导航并返回。

这是我的观点,我有3个下拉列表:

@using (Ajax.BeginForm("SearchForCars", "Home", null, new AjaxOptions { UpdateTargetId = "DivCategoriesTree", OnSuccess = "success", HttpMethod = "Post" }, new { make = "makes" }))
            {
                <div>
                    <div class="col-sm-4" style="height: 10em;display: flex;align-items: center ; padding-top:25px;">
                        <i class="fa fa-car" style="font-size:60px;color:red; padding-left:20px;"></i>
                        <strong style="padding-left:20px;">Vă rugăm să selectați vehiculul dumneavoastră!</strong>
                    </div>

                    <div class="col-sm-4">
                        <div style="padding-top:15px;">
                            <form class="form-control-static">
                                <div class="form-group">
                                      <div class="row">
                                        <div class="col-sm-10">
                                            @if (ViewData.ContainsKey("makes"))
                                            {
                                                @Html.DropDownList("makes", ViewData["makes"] as List<SelectListItem>, "--Select car--", new { @class = "dropdown-toggle form-control" })
                                            }
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-10">
                                            <p></p>
                                            @Html.DropDownList("models", new SelectList(string.Empty, "Value", "Text"), "--Select model--", new { @class = "dropdown-toggle form-control" })
                                        </div>
                                    </div>
                                    <div class="row">
                                        <p></p>
                                        <div class="col-sm-10">
                                            @Html.DropDownList("engines", new SelectList(string.Empty, "Value", "Text"), "--Select engine--", new { @class = "dropdown-toggle form-control" })
                                        </div>
                                    </div>
                                </div>

                            </form>

                        </div>
                    </div>

                    <div class="col-sm-4" style="height: 10em;display: flex;align-items: center ; padding-top:25px;">
                        <input type="submit" id="btnSearch" class="btn btn-default active" value="Cauta" disabled="disabled" style="width:150px;" />
                    </div>
                </div>
            }
        </div>

以下是3个下拉菜单:

enter image description here

我使用cookie来存储用户选择的数据。

我尝试以这种方式设置控制器的下拉列表:

 public ActionResult Index()
    {
       var asa = HttpContext.Request.Cookies.Get("make_model_engine");
        var model = asa.Values["model"].ToString();
        var make = asa.Values["make"].ToString();

        var makeList = new SelectList(makeRepository.GetMakes(), "ID", "Name");
        ViewData["makes"] = makeList;

        **var selected = makeList.Where(x => x.Value == make).First();
        selected.Selected = true;**
        return View();

    }

虽然看起来所选值是通过cookie正确设置的,但它不起作用,下拉列表没有显示所选值。 我想我应该在javascript的视图中修复它,但我是javascript的新手,我不知道什么是最好的解决方案。 你能帮帮我怎样解决这个问题? 谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将您从Cookie中读取的所选值作为SelectList constructor的最后一个参数传递:

var makeList = new SelectList(makeRepository.GetMakes(), "ID", "Name", make);