Return of incorrect AJAX Success

Asked

Viewed 225 times

0

I am using Jquery AJAX in my ASP.NET MVC application and I am having a somewhat strange problem. When performing an AJAX request, instead of retouching the content I want to load (a new ".cshtml" page) the same content is returned from the current page. I searched the console for some error, and debugged using dev tools, but I still don’t understand what’s wrong.

HTML button:

<button class="btn btn-primary-outline button-custom" id="NovoCodigo">
    <span class="glyphicon glyphicon-plus" style="color:#808080;">
    </span>
    <h4 style="display:inline; color:#808080;">Novo código</h4>
</button>

Jquery onClick:

$('#NovoCodigo').on('click', function () {
    var get_url = this.id;
    get_url = 'codigosCaixa/' + get_url + '.cshtml';
    $.ajax({
        url: get_url,
        success: function (data) {
            $('#content-load').html(data);
            },
            beforeSend: function () {
                $('#loader').css({display: "table"});
            },
            complete: function () {
                $('#loader').css({display: "none"});
            }
        });

Folder structure

Estrutura de pastas

Displayed page PS: the yellow highlight is the button pressed that calls AJAX. Note that the same content on the page is rendered where the view "codsCaixa/Novocodigo.html"
inserir a descrição da imagem aqui

EDIT:

View Code Novocodigo.cshtml:

<div class="col-md-12">
<h4 style="margin-top:50px; margin-bottom:20px;">Cadastrar código</h4>
<form action="@Url.Action("Create","CodigosCaixa")" id="form-manual" method="post" class="form-horizontal">
    //Campos do form
           .
           .
           .
    </form>
</div>
  • You should not load your cshtml this way, the correct thing would be to request your controller and action.

  • @Leandroangelo this way does not work or you meant that the ideal would be to use the controller?

  • puts in question the contents of your New view

  • So, you need to create an action in your controller to rederize this view and your get should be there, you can’t directly request the view this way, even more so that it depends on server resources. And this partial view should not be in this subderectory of Home, the ideal would be to be at the root or even in Shared

1 answer

1


If you want to keep in Homecontroller and the view is

    [HttpGet()]
    public ActionResult NovoCodigo() {

        //Adicionar a lógica necessária para rederizar sua View            

        return View("~/Views/Home/codigosCaixa/NovoCodigo.cshtml"); 
        //Adicionar uma model caso seja necessáiro.
    }

And the request you make to:

$('#NovoCodigo').on('click', function () {
var get_url = this.id;
get_url = '/Home/NovoCodigo';
$.ajax({
    url: get_url,
    success: function (data) {
        $('#content-load').html(data);
        },
        beforeSend: function () {
            $('#loader').css({display: "table"});
        },
        complete: function () {
            $('#loader').css({display: "none"});
        }
    });

Browser other questions tagged

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