Action that returns Partialview is not being called

Asked

Viewed 41 times

0

I need to present a list of results according to the form below, but the research is not presenting the result. To action in controller is not being triggered.

@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 fill the list:

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

2 answers

2

You put the verb in the method BuscarLancamentosProfissional POST, and called for an event Javascript with Jquery.load (load), who calls the Verb get, then, when you send the request to the server it is an error, because, it does not find the method with the verb correct

To solve, change [HttpPost] for [HttpGet] in the method BuscarLancamentosProfissional:

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

or

change:

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

for Jquery $.post:

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

in that case you don’t need to use the method BuscarLancamentosProfissional, because, the verb in that case it was sent correct.

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 an object. It looks like this:

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

Browser other questions tagged

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