Action that returns PartialView not being called

0

I need to present a list of results by completing the form below, but the search is not displaying the result. The action in controller is not being thrown.

@using (Html.BeginForm())
{
<div class="form-group">
    @Html.LabelFor(model => model.ProfissionalID, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("ProfissionalID", Model.Profissionais, "", new Dictionary<string, object> { { "class", "chosen-select" }, { "data-placeholder", "Selecione" } })
        @Html.ValidationMessageFor(model => model.ProfissionalID, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Pesquisar" class="btn btn-default" onclick="return efetuarPesquisa()" />
    </div>
</div>
}

<div id="resultado">

</div>

function efetuarPesquisa() {
if (!$('#ProfissionalID').val()) {
    $.MessageBox("Selecione o profissional");
    $('#ProfissionalID').focus();
    return false;
}

$("#resultado").load('/Lancamento/BuscarLancamentosProfissional/' + $("#ProfissionalID").val());
return true;
}

Action that will populate the list:

[HttpPost]
public PartialViewResult BuscarLancamentosProfissional(int profissionalId)
{
    var model = _edicaoMapper.Mapear(_lancamentoService.BuscarLancamentosProfissional(profissionalId));
    return PartialView("Edit", model);
}
    
asked by anonymous 03.10.2016 / 17:26

2 answers

2

You put verb in method BuscarLancamentosProfissional POST , and called for an event Javascript with JQuery.load (load ) , which calls the verb get , so when it sends the request to the server it gives an error, because it does not find the method with verb correct strong>

To resolve, change [HttpPost] by [HttpGet] in method BuscarLancamentosProfissional :

[HttpGet]
public PartialViewResult BuscarLancamentosProfissional(int profissionalId)
{
    var model = _edicaoMapper.Mapear(_lancamentoService
                                         .BuscarLancamentosProfissional(profissionalId));
    return PartialView("Edit", model);
}

or

Switch:

$("#resultado")
         .load('/Lancamento/BuscarLancamentosProfissional/' + $("#ProfissionalID").val());

by JQuery $ .post :

$.post( "/Lancamento/BuscarLancamentosProfissional/" + $("#ProfissionalID").val(), 
 function( data ) {
  $("#resultado").html( data );
});

In this case, you do not need to use the BuscarLancamentosProfissional method, because verb in this case was sent correct.

    
03.10.2016 / 17:40
1

The way you are using the .load () method will be done via GET. To be done via POST you must pass the parameter as object. It looks like this:

$("#resultado").load('/Lancamento/BuscarLancamentosProfissional/', { profissionalId: $("#ProfissionalID").val() }, function (){
    return true; 
});
    
03.10.2016 / 17:40