Send array to controller via Ajax.Beginform and update Partialview

Asked

Viewed 124 times

0

Good morning guys,

I have a search screen where I list several values according to the user filter.

I use Ajax.Beginform to update the table, but now I need to send an array, the user will be able to select several values in a given field, then I do not know how to send the array this way.

Does anyone know how to do it?

Thanks in advance.

Follows the codes:

Html

@using (Ajax.BeginForm(
            "Index",
            "Funnel",
            new AjaxOptions
            {
                HttpMethod = "Get",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "HistoricFunnelList",
                LoadingElementId = "loader-screen"
            }))
                {
                    <div class="row">
                        <div class="col-md-2">
                            <label for="monthAndYear">Buscar</label>
                            <select name="monthAndYear" id="monthAndYear" class="form-control input-sm">
                                <option value="1">POR MÊS E ANO</option>
                                <option value="0">POR ANO</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label for="month">Mês</label>
                            <select name="month" id="month" class="form-control input-sm">
                                <option value="1">JANEIRO</option>
                                <option value="2">FEVEREIRO</option>
                                <option value="3">MARÇO</option>
                                <option value="4">ABRIL</option>
                                <option value="5">MAIO</option>
                                <option value="6">JUNHO</option>
                                <option value="7">JULHO</option>
                                <option value="8">AGOSTO</option>
                                <option value="9">SETEMBRO</option>
                                <option value="10">OUTUBRO</option>
                                <option value="11">NOVEMBRO</option>
                                <option value="12">DEZEMBRO</option>
                            </select>
                        </div>
                        <div class="col-md-1">
                            <label for="year">Ano</label>
                            <select name="year" id="year" class="form-control input-sm">
                                @for (int i = 2016; i <= DateTime.Now.Year; i++)
                                {
                                    <option value="@i">@i</option>
                                }
                            </select>
                        </div>
                        <div class="col-md-7">
                            <label>Status</label>
                            <div class="form-control input-sm dropdowncheckbox-input">
                                <div class="dropdowncheckbox-click">
                                    <p class="dropdowncheckbox-selected">Todos</p>
                                </div>
                                <div class="dropdowncheckbox-options">
                                    <ul class="list-group no-margin">
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-all" checked="checked" type="checkbox" value="0">
                                                    Todos
                                                </label>
                                            </div>
                                        </li>
                                        <li class="line"></li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="1">
                                                    Visita inicial
                                                </label>
                                            </div>
                                        </li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="2">
                                                    Proposta apresentada
                                                </label>
                                            </div>
                                        </li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="3">
                                                    Proposta aceita
                                                </label>
                                            </div>
                                        </li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="4">
                                                    Contrato assinado
                                                </label>
                                            </div>
                                        </li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="5">
                                                    Fechado
                                                </label>
                                            </div>
                                        </li>
                                        <li class="line"></li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="6">
                                                    Aprovado
                                                </label>
                                            </div>
                                        </li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="7">
                                                    Reprovado
                                                </label>
                                            </div>
                                        </li>
                                        <li class="line"></li>
                                        <li class="list-group-item no-border">
                                            <div class="checkbox no-margin">
                                                <label>
                                                    <input class="dropdowncheckbox-checkbox" checked="checked" type="checkbox" value="8">
                                                    Perdido
                                                </label>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">
                            <label for="company">Matriz/Filial</label>
                            @Html.DropDownList("company", ViewBag.CompanyList as SelectList, new { @class = "form-control input-sm input-company-search" })
                        </div>
                        <div class="col-md-3">
                            <label for="supervisingconsultant">Supervisor de consultor</label>
                            @Html.DropDownList("supervisingconsultant", ViewBag.SupervisingConsultantList as SelectList, "---", new { @class = "form-control input-sm input-supervisingconsultant-search" })
                        </div>
                        <div class="col-md-3">
                            <label for="consultant">Consultor</label>
                            @Html.DropDownList("consultant", ViewBag.ConsultantList as SelectList, "---", new { @class = "form-control input-sm input-consultant-search" })
                        </div>
                        <div class="col-md-3">
                            <label for="supervisingsalessupport">Supervisor de suporte de vendas</label>
                            @Html.DropDownList("supervisingsalessupport", ViewBag.SupervisingSalessupportList as SelectList, "---", new { @class = "form-control input-sm input-supervisingsalessupport-search" })
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">
                            <label for="salessupport">Suporte de vendas</label>
                            @Html.DropDownList("salessupport", ViewBag.SalessupportList as SelectList, "---", new { @class = "form-control input-sm input-salessupport-search" })
                        </div>
                        <div class="col-md-4">
                            <label for="search">Pesquisa por nome, e-mail ou telefone</label>
                            <input id="search" name="search" type="text" class="form-control input-sm">
                        </div>
                        <input type="submit" value="Buscar" name="buscar" class="btn btn-sm btn-default" style="margin-top: 26px;">
                    </div>
                }

Controller

public ActionResult Index(
        int? monthAndYear = null,
        int? month = null,
        int? year = null,
        int[] status = null,
        string company = null,
        string supervisingconsultant = null,
        string supervisingsalessupport = null,
        string consultant = null,
        string salessupport = null,
        string search = null,
        int? page = null)
   {
        var user = returnUser(User.Identity.GetUserId());
        int pageNumber = (page ?? 1);
        bool monthAndYearBool = (monthAndYear == null || monthAndYear == 1);

        if (!Request.IsAjaxRequest())
        {
            var functions = returnFunctions(user);
            company = user.user_company_id;
            supervisingconsultant = functions[0];
            supervisingsalessupport = functions[1];
            consultant = functions[2];
            salessupport = functions[3];
            month = DateTime.Now.Month;
            year = DateTime.Now.Year;
        }
        else
        {
            if (ViewBag.monthAndYear != monthAndYear
                || ViewBag.month != month
                || ViewBag.year != year
                || ViewBag.status != status
                || ViewBag.company != company
                || ViewBag.supervisingconsultant != supervisingconsultant
                || ViewBag.supervisingsalessupport != supervisingsalessupport
                || ViewBag.consultant != consultant
                || ViewBag.salessupport != salessupport
                || ViewBag.search != search)
                page = 1;
        }

        ViewBag.monthAndYear = monthAndYear;
        ViewBag.month = month;
        ViewBag.year = year;
        ViewBag.status = status;
        ViewBag.company = company;
        ViewBag.supervisingconsultant = supervisingconsultant;
        ViewBag.supervisingsalessupport = supervisingsalessupport;
        ViewBag.consultant = consultant;
        ViewBag.salessupport = salessupport;
        ViewBag.search = search;

        var historicFunnel = _historicFunnelAppService.ListHistoricFunnel(monthAndYearBool, month, year, status, company, supervisingconsultant, supervisingsalessupport, consultant, salessupport, search, pageNumber);
        var pagedViewModel = Mapper.Map<IEnumerable<HistoricFunnel>, IEnumerable<HistoricFunnelViewModel>>(historicFunnel.ToArray());
        var historicFunnelViewModel = new StaticPagedList<HistoricFunnelViewModel>(pagedViewModel, historicFunnel.GetMetaData());

        foreach (var i in historicFunnelViewModel)
        {
            var x = returnUser(i.historicfunnel_supervisingconsultant);
            i.historicfunnel_supervisingconsultant = returnName(x.user_name);

            x = returnUser(i.historicfunnel_consultant);
            i.historicfunnel_consultant = returnName(x.user_name);

            switch(i.historicfunnel_statusnegotiation)
            {
                case (1):
                    i.historicfunnel_statusnegotiationdescription = "(10%) - " + i.historicfunnel_count + "ª visita inicial.";
                    break;
                case (2):
                    i.historicfunnel_statusnegotiationdescription = "(25%) - Proposta apresentada.";
                    break;
                case (3):
                    i.historicfunnel_statusnegotiationdescription = "(50%) - Proposta aceita.";
                    break;
                case (4):
                    i.historicfunnel_statusnegotiationdescription = "(75%) - Contrato assinado.";
                    break;
                case (5):
                    i.historicfunnel_statusnegotiationdescription = "(100%) - Contrato fechado.";
                    break;
                case (6):
                    i.historicfunnel_statusnegotiationdescription = "Contrato aprovado.";
                    break;
                case (7):
                    i.historicfunnel_statusnegotiationdescription = "Contrato reprovado.";
                    break;
                case (8):
                    i.historicfunnel_statusnegotiationdescription = "Perdido.";
                    break;
            }
        }

        if (Request.IsAjaxRequest())
            return PartialView("_HistoricFunnel", historicFunnelViewModel);

        string[] names = user.user_name.Split(' ');
        var systemFunction = new Functions().FunctionsList().First(x => x.Id == user.user_function);

        ViewBag.User = user;
        ViewBag.FirstName = names[0].ToLower();
        ViewBag.LastName = names[names.Length - 1].ToLower();
        ViewBag.Photograph = (string.IsNullOrEmpty(user.user_photograph)) ? "/assets/usersPhoto/noPhoto.png" : user.user_photograph;
        ViewBag.Function = systemFunction.Name;

        ViewBag.CompanyList = new SelectList(_companyAppService.GetAll(), "company_id", "company_fantasyname", company);
        ViewBag.SupervisingConsultantList = new SelectList(_userAppService.CollaboratorList(company, null, 2), "Id", "user_name", supervisingconsultant);
        ViewBag.ConsultantList = new SelectList(_userAppService.CollaboratorList(company, supervisingconsultant, 4), "Id", "user_name", consultant);
        ViewBag.SupervisingSalessupportList = new SelectList(_userAppService.CollaboratorList(company, null, 3), "Id", "user_name", supervisingsalessupport);
        ViewBag.SalessupportList = new SelectList(_userAppService.CollaboratorList(company, supervisingsalessupport, 5), "Id", "user_name", salessupport);

        return View(historicFunnelViewModel);
    }
  • It’s the list of Checkboxes you want to send, right?

  • This msm, the user can send all or only one or two and etc

1 answer

2


So that your inputs of status can be sent via Get form, you should name them as your possible on Array.

name="status[0]" id="status[0]"
  • The part of the da array, which I don’t know about, is to send Aray to the controller.

  • it will be sent via the parameter int[] status = null that you defined in your Action

  • If I rename it like this it’s gone?

  • I’ll test it here.

  • It didn’t work Pablo.

  • I’ll build an example using typed view.

  • I put without the clasps, it worked agr. VLW!

  • 2

    But I advise you to use typed view for what you’re doing.

  • The view I’m using is typed. I only put the search form part in html.

  • See this answer http://answall.com/a/102945/5846

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.