Cannot get json data with ajax in Razor Pages [duplicate] - asp.net-core

This question already has answers here:
Example AJAX call back to an ASP.NET Core Razor Page
(8 answers)
Closed 5 years ago.
i am trying to get some data from Razor Pages in ASP.NET Core 2.0.
But the problem is that it does not returns any data.
I also tried debugging it, it does not fire that method (OnGetProducts) at all.
The model Index.cshtml.cs:
private IProductRepository _productRepository;
public IndexModel(IProductRepository repository)
{
_productRepository = repository;
}
public void OnGet()
{
}
public IActionResult OnGetProducts(int page)
{
var model = _productRepository.GetProducts().Skip(page * 10).Take(10);
return new JsonResult(model);
}
the razor page Index.cshtml
<div id="products">
</div>
#section scripts{
<script>
$(function () {
getProducts(0);
});
var isInitialized = false;
function getProducts(page) {
$.ajax({
type: 'GET',
url: "Products",
contentType: "application/json",
dataType: "json",
data: {
handler: 'Products',
page: page
},
success: function (datas) {
console.log(datas);
}
});
}
</script>
}
p.s. this page in in folder Pages/Products/Index.cshtml(.cs)

I usually use razor functions to generate URLs instead of hard coding them in js. If your action is not even being triggered, assuming that you are not accidentally in release mode, it is because the URL doesn't point to the right location. First of all set js variables in razor something like this:
var productsURL = #Url.Context("~/products");
Also run yourdomain/products in your browser and if you get a 404.
Alternatively I use this function to directly use c# objects in js:
public static IHtmlContent ToJS(this IHtmlHelper htmlHelper, object obj)
=> htmlHelper.Raw(JsonConvert.SerializeObject(obj));
With this function created in a static class, you can also create a js object directly something like this:
<script>
var products = #Html.ToJS(repository.GetProducts().Skip(page * 10).Take(10));
</script>
Of course this will only create the object in page load, if you want it to change after page load, you can consider creating a partial view via ajax. Also note that the second alternative will be slower than the first for ajax.

Related

How to make an admin ajax call in prestashop 1.7.6

I'm trying to make an ajax call in Prestashop Admin:
I created a module without a config page. It just add a button in some backoffice page, I'm trying to make an ajax call to my module file without success.
Making an ajax call in frontend is working (I added an ajax.php file in my modules/mymodule/controller/front/ directory), I tried to do the same thing for admin but it's not working at all.
What I've done:
loading the js file from actionAdminControllerSetMedia is ok
adding this in the composer.json file:
"autoload": {
"psr-4": {
"MyModule\\Controller\\": "controllers/admin/"
},
"config": {
"prepend-autoloader": false
},
created the controllers/admin/ajax.php file with this code (based on this documentation code):
namespace MyModule\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
class DemoController extends FrameworkBundleAdminController
{
public $auth = false;
public $ssl = true;
public $ajax = true;
public $errors = false;
public $message;
public function __construct()
{
parent::__construct();
}
public function initContent()
{
parent::initContent();
}
public function postProcess()
{
PrestaShopLogger::addLog("MODULE CONTROLLER OK ", 1);
}
public function displayAjax()
{
$this->ajaxDie(json_encode(array('success'=> !$this->errors, 'message' => $this->message)));
}
}
Then I tried to call the ajax from different way in js but never worked (the post query return is a message from prestashop "page not found" with http 200 response.
the doc isn't very helpful and I only find old messages/ways to do (from Prestashop 1.7.5 I'd be able to create a custom Admin controller but it doesn't work), can someone explain me the steps to follow?
thanks
Assuming it is for a PS1.7+ module, using Symphony:
Declare a link in a method of your admin controller (src/Controller/Admin) e.g
$adminLink = $this->generateUrl()
and return in with:
return $this->render
In your views/js/back.js"
$.ajax({
url: adminLink,
type: 'POST',
async: false,
data: {
},
success: (data) => {
}
});
Note: check the generateUrl and render functions for the necessary arguments.

How to add client click event to div to call method in ASP.NET Core 2.2 Razor page

I made a calendar in a Razor page, and I want to make each date (a div) clickable so they call a method and pass it the clicked date (div id set to date). I'm generating the calendar in the cs page and I'm not using MVC controllers.
#model Budget.Pages.CalendarModel
#{
ViewData["Title"] = "Calendar";
}
<form method="post">
#Html.Raw(Model.getCal())
</form>
And then in my cs page I have the method getCal() that generates a calendar via divs, css and some math, which is working fine, but I need to attach onClick events to each day (div).
public string getCal()
{
//I won't print out all of my calendar generation code in ordfer to simplify this question.
//The code below happens in a loop where the MM, DD and YYYY change as appropriate to be
//unique. This is where I want to put my onclick events to call another method, onDateSelect(this.id)
retValue += "<div id='" + MM + "_" + DD + "_" + YYYY + "' class='col-md-9 dayCell'>" +
strDayNo +
"</div>";
return retValue; //When out of loop of course
}
After rendering the content with #Html.Raw(Model.getCal()) in your page , you can add click event on your div :
#section Scripts{
<script>
$(document).on('click', ".dayCell", function () {
});
</script>
}
Razor Pages are designed to be protected from (CSRF/XSRF) attacks. Hence, Antiforgery token generation and validation are automatically included in Razor Pages. Please refer to below article for code sample :
Handle Ajax Requests in ASP.NET Core Razor Pages
Here is code sample based on your requirement :
#section Scripts{
<script>
$(document).on('click', ".dayCell", function () {
$.ajax({
type: "POST",
url: "/YourPageName?handler=Send",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify({
ID: this.id
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response);
}
});
});
</script>
}
Server side function :
public JsonResult OnPostSend([FromBody]PostData value)
{
....
}
public class PostData
{
public string ID { get; set; }
}
Also configure the antiforgery service to look for the X-CSRF-TOKEN header:
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");

Refresh Page on Back Click - MVC 4

What would be a simple way to force my ActionResult to always fire when I hit this page? On browser back click, for instance, this will not execute.
public ActionResult Index()
{
//do something always
return View();
}
Disabling cache on the ActionResult forces the page to refresh each time rather than rendering the cached version.
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public ActionResult Index()
{
//do something always
return View();
}
Now when you click the browsers back button, this is hit every time.
You could try the onunload event and ajax:
<script>
window.onunload = function () {
$.ajax({
url: '/ControllerName/Index',
type: 'POST',
datatype: "json",
contentType: "application/json; charset=utf-8"
});
};
</script>
Adding this code to my HTML works just fine for me:
<input id="alwaysFetch" type="hidden" />
<script>
setTimeout(function () {
var el = document.getElementById('alwaysFetch');
el.value = el.value ? location.reload() : true;
}, 0);
</script>
This might come handy for those who prefer not to deal with server side code as opposed to the accepted solution.
All it does is assign a value to a hidden input on first load which will remain on the second load in which case the page is force refreshed.

MVC 4 View Knockout Bindings not updating on ajax call

I have gone through as many questions on here as I could find and tried all the different suggestions and cannot get this to work. I have a view that is bound with Knockout using the mapping plugin and it works okay but only when I do the "wrong thing". Everything that I have read says that you should only make one call to ko.applyBindings() per view and then everything should update using ko.mapping.fromJS(). I cannot seem to get this to work, the only way I have been able to get my view to refresh is to call ko.applyBindings() again in the success call back from my .ajax() call. Here is the offending code.
<script type="text/javascript">
var viewModel;
$(document).ready(function() {
$("#panelbar").kendoPanelBar({
expandMode: "multiple"
});
$.ajax({
type: 'GET',
url: '/Home/IsUserMarketingManager',
success: function (data) {
if (data == true) {
$('#submitNewCase').hide();
$('#approveCase').show();
$('#disapproveCase').show();
}
}
});
// Generate client View Model from Server View Model
viewModel = new ViewModel();
ko.mapping.fromJS(#Html.Raw(Json.Encode(Model)),{}, viewModel);
ko.applyBindings(viewModel);
});
function ViewModel () {
var self = this;
self.addLocation = function() {
self.AdditionalLocations.push({ GaNumber: "" });
};
}
</script>
And later this to update the form with retrieved data:
<script type="text/javascript">
$('#btnImport').click(function () {
$.blockUI({ message: '<h2>Importing Client Information...</h2> <img src="/Images/ajax-loader.gif"><br />' });
$.ajax({
type: 'post',
url: '/Home/ImportClientCrmInfoJson',
dataType: "json",
data: ko.mapping.toJS(viewModel),
success: function (data) {
$.unblockUI();
if (!data.AccountNull) {
ko.mapping.fromJS(data, {}, viewModel);
} else {
alert("Could not find account for this GA Number, please try again.");
}
}
});
});
</script>
When submitting the form to my controller, all the data is there and mapped correctly to my server side View Model, but the form in the view isn't updated with the data that comes back from the $.ajax call. I've gotten the form to update if I do the following, but I know it's not the right way and has caused me other issues as well.
<script type="text/javascript">
$('#btnImport').click(function () {
$.blockUI({ message: '<h2>Importing Client Information...</h2> <img src="/Images/ajax-loader.gif"><br />' });
$.ajax({
type: 'post',
url: '/Home/ImportClientCrmInfoJson',
dataType: "json",
data: ko.mapping.toJS(viewModel),
success: function (data) {
$.unblockUI();
if (!data.AccountNull) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel); // This works but isn't the right way...
} else {
alert("Could not find account for this GA Number, please try again.");
}
}
});
});
</script>
Any help would be much appreciated.
Have you examined that the following line of code appears to create a 'NEW' viewmodel?
viewModel = ko.mapping.fromJS(data);
When you do this the new viewModel the old bindings are destroyed. This is why you have to call ApplyBindings again. Anyway, I think the above line of code is the root of the problem.
Is there a way for you to create an observable property on the viewModel and allow the viewModel to reflect the data in this object? That may be a more practical approach to the update process.
In the success callback of the ajax call, use this method ko.applyBindings(viewModel) but pass as a second parameter the DOM portion you want to update as follows
ko.applyBindings(viewModel, $("#mydiv")[0])
Don't use a jquery object but a REAL DOM object.

load an html file into a panel

I am new to sencha touch 2.0. I have an html file. I am trying to load this html file(or content) into a panel. I am simply using an ajax call but its not working. Following is the code.
This is the html file i am running in the browser.
index.html:
<script type="text/javascript" src="touch/sencha-touch-debug.js"></script>
<script type="text/javascript" src="HTMLPanel.js"></script>
<script type="text/javascript" src="app.js"></script>
this is app.js:
Ext.setup({
name : 'SampleLoad',
requires : ['HTMLPanel'],
launch : function () {
var HTMLPanel = new HTMLPanel({
scroll : 'vertical',
title : 'My HTML Panel',
url : 'sample.html'
});
}
});
and this is HTMLPanel.js:
//HTMLPanel = Ext.extend(Ext.Panel, { //gives error
var HTMLPanel = Ext.define('HTMLPanel',{
extend : 'Ext.Panel',
constructor : function( config ) {
HTMLPanel.superclass.constructor.apply(this, arguments);
// load the html file with ajax when the item is
// added to the parent container
this.on(
"activate",
function( panel, container, index ) {
if( this.url && (this.url.length > 0) )
{
Ext.Ajax.request({
url : this.url,
method : "GET",
success : function( response, request ) {
//console.log("success -- response: "+response.responseText);
panel.update(response.responseText);
},
failure : function( response, request ) {
//console.log("failed -- response: "+response.responseText);
}
});
}
},
this
)
},
url : null
});
I just want the html content to be displayed within the panel. Can someone help?
The class system has changed quite a lot in Sencha Touch 2 compared to 1.x. It is now very similar to how ExtJS 4 is. The idea behind the changes is to make it easier to understand, quicker to develop and more dynamic.
Some changes:
you should no longer use new HTMLPanel({}). Instead, use Ext.create, i.e. Ext.create('HTMLPanel', {}).
you should no longer use Ext.extend to define your custom classes. Instead, use Ext.define with an extend property.
you do not need to use HTMLPanel.superclass.constrctor.apply(this, arguments) anymore to call the parent. Instead, you can use this.callParent(arguments)
you should very rarely override constructor. This is bad practise. Instead, you should use the config block:
Ext.define('HTMLPanel', {
extend: 'Ext.Panel',
config: {
html: 'This is the html of this panel.'
}
});
Please note that configurations only go within the config block when you define a new class using Ext.define. If you are creating a new instance of a class using Ext.create, new ClassName or using an object with an xtype, configurations do not need to be within the config object.
You can find out more information about the new class system by reading this guide. There is also a great guide on how to migrate from 1.x to 2.x here.
So, lets make your code work.
index.html (nothing needs to change):
<script type="text/javascript" src="touch/sencha-touch-debug.js"></script>
<script type="text/javascript" src="HTMLPanel.js"></script>
<script type="text/javascript" src="app.js"></script>
app.js:
// You should use Ext.application, not Ext.setup
Ext.application({
name: 'SampleLoad',
requires: ['HTMLPanel'],
launch: function () {
var HTMLPanel = Ext.create('HTMLPanel', {
// this is now `scrollable`, not `scroll`
//scroll: 'vertical',
scrollable: 'vertical',
url: 'sample.html'
});
// Add the new HTMLPanel into the viewport so it is visible
Ext.Viewport.add(HTMLPanel);
}
});
HTMLPanel.js:
// You do not need to save a reference to HTMLPanel when you define your class
//var HTMLPanel = Ext.define('HTMLPanel', {
Ext.define('HTMLPanel', {
extend: 'Ext.Panel',
// We are using Ext.Ajax, so we should require it
requires: ['Ext.Ajax'],
config: {
listeners: {
activate: 'onActivate'
},
// Create a new configuration called `url` so we can specify the URL
url: null
},
onActivate: function(me, container) {
Ext.Ajax.request({
// we should use the getter for our new `url` config
url: this.getUrl(),
method: "GET",
success: function(response, request) {
// We should use the setter for the HTML config for this
me.setHtml(response.responseText);
},
failure: function(response, request) {
me.setHtml("failed -- response: " + response.responseText);
}
});
}
});
Hopefully this helps.
rdougan's answer worked for me. If it still doesn't work for you, check out this example from the Sencha Docs where they're loading .js files using AJAX a slightly different way (it would be exactly the same for .html files). To get the source, download the Sencha Touch 2 SDK and it will be under examples/nestedlist.