选择另一个下拉菜单后,Ajax调用MVC下拉菜单-刷新后保持选中状态

时间:2019-04-05 17:42:05

标签: javascript c# ajax asp.net-mvc

我有一个MVC下拉列表。选择后,将在AJAX调用中调用另一个Dropdownlist。其工作正常,但是在页面刷新时,我缺少所选的选项。

我的视图-下拉列表1

<div class="col-lg-2 col-sm-2 form-group">
    <label class="font-noraml">Empresa</label>
     <div class="input-group">
          @Html.DropDownList("IdCliente", null, "SELECIONE A EMPRESA", htmlAttributes: new { @class = "form-control combo2", required = true, @id = "IdCliente", @onchange = "javascript:GetFiltros(this.value);" })
     </div>
</div>

我的视图-下拉菜单2

<div class="col-sm-2">
     <label>Condomínio</label>
      <select disabled id="codCond" name="codCond" style="width: 100%">
            <option selected="selected">TODOS</option>
       </select>
</div>

MY JS AJAX

if ($("#IdCliente").val() != '') {
                GetFiltros($("#IdCliente").val());
            }

function GetFiltros(_idCLiente) {

            $.ajax({
                url: "/Analytics/FiltroCondominio/",
                data: { cliente: _idCLiente },
                cache: false,
                type: "POST",
                success: function (data) {                   
                    if (data == "") {
                        swal({ type: "warning", title: "Alerta", text: "Esta Empresa não possui configuração de Conexão, por favor entre em contato com a equipe de suporte.", timer: 10000, showConfirmButton: true });
                    }
                    else {
                        var $codCond = $('#codCond');
                        $codCond.empty();
                        $('#codCond').prop("disabled", false);
                        $("#codCond").append("<option value=''>TODOS</option>");
                        for (var x = 0; x < data.length; x++) {
                            $('<option>', { value: data[x].Value }).html(data[x].Text).appendTo($codCond);
                        };
                    }
                },
                error: function (reponse) {
                    alert("error : " + reponse);
                }
            });
        }

我的控制器

[HttpPost]
public ActionResult FiltroCondominio(string cliente)
{
     var cond = new SelectList(new AnalyticsData().GetFiltroCond(cliente), "CD_CLIENTE", "CONDOMINIO");

      return Json(cond);
}

当我选择该选项并按下加载按钮时,它会带来结果,并在刷新时使下拉列表2再次被选中(TODOS)。

我的MVC GET

 public ActionResult AcessosUsuarios()
        {
            var cliData = new LicenciadoData();
            var usuario = new UsuarioData();
            var _userId = User.Identity.GetUserId();           
            string cliente = cliData.GetPartnerId(usuario.GetId_Cliente(_userId));

            var agora = DateTime.Now;
            var start = agora.AddDays(-30);
            var end = agora.AddDays(-1);

            ViewBag.Data1 = start.ToShortDateString();
            ViewBag.Data2 = end.ToShortDateString();               
            ViewBag.ListaUsuarios = "";
            ViewBag.IdCliente = new SelectList(cliData.ListarPainel(_userId), "Id", "Fantasia");

            if (User.IsInRole("CLIENTE"))
            {
                var IdCliente = new UsuarioData().GetId_Cliente(User.Identity.GetUserId());
                ViewBag.codCond = new SelectList(new AnalyticsData().GetFiltroCond(cliente), "CD_CLIENTE", "CONDOMINIO");   
            }
            return View();
        }

我的MVC帖子

[HttpPost]
        public ActionResult AcessosUsuarios(string data1, string data2, int? codCond, int? IdCliente, string listaCheia)
        {
            if (!string.IsNullOrEmpty(IdCliente.ToString()))
            {
                var cliData = new LicenciadoData();
                string cliente = cliData.GetPartnerId(IdCliente);

                var AcessosUsuarios = new List<ListAcess>();
                var ListaUsuarios = new List<ListUserAccess>();
                var TotalUsuarios = new List<UserTotal>();
                var TotalAcessos = new List<ListAcess>();

                AcessosUsuarios = new AnalyticsData().GetAcessoUsuarios(cliente, codCond, data1, data2);
                ListaUsuarios = new AnalyticsData().GetListaUsuarios(cliente, codCond);

                ViewBag.ListaUsuarios = ListaUsuarios;
                ViewBag.Condominio = new AnalyticsData().GetCondominio(cliente, codCond);

                ViewBag.Data1 = data1;
                ViewBag.Data2 = data2;
                ViewBag.Filtro = codCond ?? 0;

                ViewBag.IdCliente = new SelectList(cliData.ListarPainel(User.Identity.GetUserId()), "Id", "Fantasia");
                ViewBag.codCond = new SelectList(new AnalyticsData().GetFiltroCond(cliente), "CD_CLIENTE", "CONDOMINIO", codCond);

                if (User.IsInRole("CLIENTE"))
                {
                    ViewBag.codCond = new SelectList(new AnalyticsData().GetFiltroCond(cliente), "CD_CLIENTE", "CONDOMINIO", codCond);
                }
            }
            else
            {
                var cliData = new LicenciadoData();

                TempData["cliente"] = "0";
                ViewBag.IdCliente = new SelectList(cliData.Listar(User.Identity.GetUserId()), "Id", "Fantasia");

                if (User.IsInRole("CLIENTE"))
                {
                    ViewBag.codCond = new SelectList(new KiperServerData(IdCliente).GetListaCond(), "Cod", "Nome", codCond);
                }
            }

            return View();
        }

0 个答案:

没有答案