Url.Action in asp.net core - asp.net-core

I like the new tag helpers attributes which make readable dynamic html contents. I can create a link by using the asp-controller and asp-action attributes like this:
<a asp-controller="Home" asp-action="Index">Index</a>
The problem is that there are cases where I need just the URL of the action in order to use it my javascript. For example in order to make an ajax call in ASP.NET MVC 5, I could have the following code:
$.ajax({
type: "POST",
url: '#Url.Action("GetData","Ajax")',
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
}
});
I tried to find an equivalent method in ASP.NET Core but I was not able to find one so I have to create the URL myself. Is there a way to get the Action and Razor Page URL in ASP.NET Core.

I just found how to do it. In case someone else is looking for the same thing here is how to do it.
#this.Url.Action("Index", "Home")

Related

#Ajax.ActionLink in ASP.NET CORE [duplicate]

Earlier in MVC I used #Ajax.ActionLink for Ajax call and replaced container in my layout.
Now in .Net Core there is anything like AjaxHelper back then.
How can I form Ajax call without writing jquery script for every menu item in my dashboard.
I tried #Url.Action with anonymous Ajax parameters but that won't work.
#Url.Action("Index", "User", new
{
data_ajax = "true",
data_ajax_method = "GET",
data_ajax_mode = "replace",
data_ajax_update = "#replace"
}))"
No. Honestly, you never needed it anyways. All it did was create a regular link and then add a trivial bit of JavaScript you can easily add yourself. Long and short, let it go:
<a class="menu-item" asp-action="Index" asp-controller="User">Click Me</a>
Then:
<script>
$('.menu-item').on('click', function (e) {
e.preventDefault();
$.get(this.href, function (html) {
$('#replace').html(html);
});
});
</script>
By binding to the class, you only need this one piece of JS for any link with a menu-item class.

How to invoke a backend function in JS in a Smarty template used in a Prestashop module?

In a custom prestashop module, I am trying to dynamically show children categories in a dropdown.
This is the code I add in the hook before calling the template:
$subcatObj = new Category("24");
$subcatObj2 = $subcatObj->getSubCategories($this->context->language->id);
$this->context->smarty->assign('seriesCategories', $subcatObj2 );
This is how I use this in the template:
<select id="series_dropdown" class="selectpicker" data-style="btn-primary">
{foreach from=$seriesCategories item=seriesCategory}
<option value="{$seriesCategory.id_category}">{$seriesCategory.name}</option>
{/foreach}
</select>
What I need is to invoke this getSubCategories with different values from JS, to dynamically populate the dropdown. So instead a hardcoded 24, I would like to use a JS variable.
$subcatObj = new Category(******** JAVASCRIPT VARIABLE *********);
$subcatObj2 = $subcatObj->getSubCategories($this->context->language->id);
What shall be done to achive this? -It is sort kind of AJAX web service-
Prestashop 1.7.1
You need create a php file in your module to handle the ajax request, eg:
/modules/your_module/ajax.php
<?php
require_once(dirname(__FILE__).'/../../config/config.inc.php');
$subcatObj = new Category((int)Tools::getValue('id_category'));
$subcatObj2 = $subcatObj->getSubCategories((int)Context::getContext()->language->id);
die(Tools::jsonEncode($subcatObj2));
Now in JavaScript inside the .tpl, or by a .js file loaded there:
$.ajax({
url: '/modules/your_module/ajax.php',
type: 'POST',
dataType: 'JSON',
data: { id_category: $('#series_dropdown').val() }
})
.done(function(data) {
console.log(data); // 'data' should contain the response of corresponding sub-categories
})
.fail(function() {
console.log('An error has occurred!');
});
PD. This is a basic example, you should add some security token in the ajax.

how to pass file from client to controller by jquery?

i want to create very simple sample to uploading a file by client side in mvc4 by jquery and java script.
i Google it and found many samples and many plugins on internet but i prefer to do not have any dependency on any extra plugin or library like "uploadify"
for this i create a simple mvc4 application and in my view i attach my script file that contains method bellow until user click a button on this view start to uploading.
i do not know how to change bellow method to pass file to controller(in client side) ?
function uploadimage() {
$.ajax({
url: "/Uploader/FileUpload",
type: 'POST',
dataType: 'json',
data:null,
contentType: 'application/json; charset=utf-8',
success: function (msg) {
},
error: function (xhr) {
}
});
}
in my view
<input type="file" id="fileToUpload" name="file" />
<input type="button" value ="Upload" onclick="uploadimage()"/>
my controller
public ActionResult FileUpload(HttpPostedFileBase file)
{
//do somethings with file
}
file upload is not possible through ajax. You can upload file, without refreshing page by using IFrame.

double click on mvc 4 web grid

I am preparing a mvc 4 application and I am pretty new to it. I would like to implement a functionality like by double clicking a row of mvc 4 webgrid I should call an action method in ajax. But unfortunately I could not find how to implement double click on mvc 4 web grid.
Can you please help me on that?
You could use the .dblclick() event in jQuery. For example:
<script type="text/javascript">
$(function() {
$('table td').dblclick(function() {
$.ajax({
url: '#Url.Action("SomeAction", "SomeController")',
type: 'POST',
success: function(result) {
// do something with the result from your AJAX call
}
});
});
});
</script>
Obviously there are lots of improvements that could be done to this code. For example you could use HTML5 data-* attributes on your grid to specify the url to the controller action that needs to be invoked and then externalize this script in a separate javascript file. You might also need to adjust the jQuery selector to match your WebGrid element.

Rails 3 loading page with ajax animation

i have already read this topix : "http://stackoverflow.com/questions/4820637/how-do-i-perform-an-ajax-request-to-the-create-action-on-loading-a-page-in-rails" but it doesn't respond to the question..
I want to make a loading animation into my rails 3 app cause the server takes time to send email and do some other calculation so the user experience is not so good...
I have read this tutorial : http://www.marketingformavens.com/blog/create-a-page-loading-animation-with-ruby-on-rails-and-ajax but it's for rails2 (remote_function doesn't exist anymore in rails 3).
I wonder if someone can tell the way i should do so :
-> having a nice animation loaded onto the screen when the user click on an action (like create, or update)
Thank's a lot.
Hope to find a answer...
If I understand you want to load an animation while your action is done in ajax. Just use the jQuery API to call your controller actions in Ajax and use callbacks to stop your animations :
$("#myaction").click(function(){
//write your animation code here
$.ajax({
url: "http://localhost:3000/yourcontrolleraction",
dataType: "json",
type: "POST",
processData: false,
contentType: "application/json",
data: yourdata
success: function(){
//stop your animation here
}
})
});
For more on using jQuery Ajax with Rails I suggest your read this excellent post : http://blog.project-sierra.de/archives/1788