Retrieves data with Request.QueryString

3

I have a Partial that loads the data:

@using (Html.BeginForm("PesquisarEventoPorLocal", "CadastroEvento", FormMethod.Get))
{
    <div class="row">
        <div class="col-sm-12 col-md-12">
            <div class="form-group col-md-3">
                <label for="sel2">Pesquisar Por local:</label>
                @Html.DropDownList("Pesquisa:", ViewBag.TiposLocal as SelectList, new { @class = "form-control", id = "comboBox", name= "comboBox" })
            </div>
            <div class="form-group  col-sm-3 col-md-3">
                <br/>
                <button type="submit" class="btn btn-danger">Pesquisar</button>
            </div>
        </div>
    </div>


}

The controller is not passing the data.

        [HttpGet]
        public ActionResult PesquisarEventoPorLocal()
        {
            string CodigoLocal = Request.QueryString["PesquisarEventoPorLocal"];
            string CodigoLocal1 = Request.QueryString["Pesquisa"];
            string CodigoLocal2 = Request.QueryString["comboBox"];

            return RedirectToAction("index", "CadastroEvento");
        }
    
asked by anonymous 18.08.2017 / 06:56

2 answers

1

You have some errors in your code.

These fields below do not exist in your form, so they exist you must create an input or select and add the name attribute and set a name for the element.

  

SearchEventoPorLocal

     

Search

     

comboBox

Note: The code below does not work the way you are expecting

@Html.DropDownList("Pesquisa:", ViewBag.TiposLocal as SelectList, new { @class = "form-control", id = "comboBox", name= "comboBox" })

Primarily these two attributes:

id = "comboBox", name= "comboBox"

When you create a @Html.DropDownList , the first parameter is the id and the name element, I have not yet seen how you force the element to change id and the name passed this data by the htmlAttribute parameter. So when you give submit in the form you can not get the query Request.QueryString["comboBox"];

string CodigoLocal2 = Request.QueryString["comboBox"];

This code will be correct if you do it this way:

<div class="form-group col-md-3">
    <label for="sel2">Pesquisar Por local:</label>
    @Html.DropDownList("comboBox", ViewBag.TiposLocal as SelectList,"Pesquisa:", new { @class = "form-control" })
</div>
    
18.08.2017 / 18:38
1

In your BeginForm you say that you should submit the data to CadastroEvento .

But in your controller, you expect data in PesquisarEventoPorLocal .

Suggestion for improvement

[HttpGet]
public ActionResult PesquisarEventoPorLocal(string pesquisarEventoPorLocal, string pesquisa, string comboBox) {...}
    
18.08.2017 / 08:51