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
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"
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.
– Leandro Angelo
@Leandroangelo this way does not work or you meant that the ideal would be to use the controller?
– Csorgo
puts in question the contents of your New view
– Leandro Angelo
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
– Leandro Angelo