What is the best way to use Ajax GET after dropping an item with jQuery, and then append the result of a PartialView

In a webpage, I have

<ul id="myPicturesAlbum" > 
    <li id="picture1" data-index-number="image1"></li>
    <li id="picture2" data-index-number="image2"></li>
    <li id="picture3" data-index-number="image3"></li>
</ul>

list of thumbnails/images and where users can drag and drop those image thumbnails.

After the OnDrop() event: How can I initiate an Ajax call to the ASP MVC Action Method, passing the id of the list item.

Then insert the returned ActionResult from the partial view/View as a nested div inside the container.

<div id="container"></div> 

    $("#container").droppable({ 
        accept: "#container",
        drop: function( event, ui ) {
            //how do we make the ajax call with the item id?
        } 
});

How can I encapsulate this within an onDrop Call with a distinct Id for the item

$.ajax({
    type: "GET",
    url: "/Home/GetMyPartialView/",
    data: ThumNailId,                //** how do I connect this to the drag and drop**
    success: function (myresult) {     
        if (myresult.Status === 300) { //300 is just an arbitrary value
            showPopup("You do not have permission to access that.");
        }

        $("#Container").html(myresult.ViewString); //the HTML retrieved from the controller
    },
    error: function (errorData) { onError(errorData); }
});

Should I utilize Html.Partial or Partial View in this scenario?

Answer №1

When an element is dragged, the ui parameter holds relevant information that can be utilized as follows:

$("#container").droppable({ 
    accept: "#container",
    drop: function( event, ui ) {
        var id = ui.draggable.data('index-number'); // This will return 'picture1' if the 1st li element is dragged and dropped
        // Alternatively, you can use var id = ui.draggable.attr('id'); to get the value 'pic1'
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetMyPartialView", "Home")', // Avoid hard coding URLs for flexibility
            data: { ThumNailId: id },
            ....
        });     
    } 
});

It's important to note that this code assumes your method has a signature like:

public ActionResult GetMyPartialView(string ThumNailId)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pass various chosen checkboxes to Java by utilizing jQuery and Ajax technology

I am struggling with sending multiple selected checkboxes from HTML to Java using jQuery/Ajax. The issue is that when I check the result in Java, instead of getting the values I selected (e.g., National, State), I get "activityRangeCBs[]". Here is the HTM ...

Is it no longer necessary to bind functions in React Component Classes?

Recently, I observed that when defining normal class component functions in React, there is no longer a need to bind them inside the class constructor. This means that even without using ES6 public class field syntax, you can simply pass these functions to ...

Show a jQuery box when hovering and disappear when the mouse leaves

I'm currently working on creating a box that pops out when a link is hovered over. Although I have the basics covered, I've been struggling to fully achieve my desired outcome. You can view my progress here: http://jsfiddle.net/EpV87/1/ (please ...

CSS rotation causing issues with absolute positioning

In the example below, I have demonstrated the current behavior I am experiencing and the desired outcome. // Rotated div rotated.style.left = "50px"; rotated.style.top = "100px"; // Original "untouched" div original.style.left = "50px"; original.style.t ...

What is the proper way for AJAX to function in WordPress when there is no output function available?

I am looking to incorporate AJAX functionality into my WordPress site to make a call to a third-party API. The goal is to update the state of some checkboxes based on the response received. While I have experience with AJAX, my previous implementations in ...

jQuery's element loading function fails to work with ajax requests

When trying to preload ajax data before attaching it to a div, I utilized the following code: $.ajax({ url: 'ajax.php', data: { modelID:id }, type: 'post', success: function(result){ $(result).load(function(){ ...

Is Q.js capable of functioning independently from node.js and require()?

Currently experimenting with the latest version of q.js to integrate promises into my ajax calls without utilizing node.js at all. I retrieved the most recent version from https://github.com/kriskowal/q and only included q.js in my project. While testing, ...

Configuring CORS in an Angular JS controller

Having a controller with a service that retrieves JSON from another server, I encountered the following issue: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:somesite.com. This can be fixed by moving the ...

The transfer of JSON data to PHP through AJAX is not successful

I have encountered an issue while attempting to send a JSON string to a PHP file using ajax. The problem lies in the variable I am posting not being delivered when employing the JQUERY ajax method as illustrated below: <DOCTYPE html> <html> ...

Value-based JavaScript object prototype

Is there a way to ensure that every new object I create has its own unique dataTransfer property? For instance, when I try something like Object.prototype.dataTransfer = new DataTransfer();, I want a.dataTransfer.prop to remain 1 even if b.dataTransfer.pro ...

Ways to Manage modals responses as services in AngularJS

I am a beginner in the world of JavaScript and AngularJS. I am fascinated by some of the concepts within JS, like trying to create a modal service using the example I found online. I am eager to delve deeper into what exactly is happening under the hood, e ...

Angular - How child components can communicate with their parent components

I'm struggling to understand how to communicate between components and services. :( Even though I've read and tried a lot, some examples work but I still don't grasp why (?). My goal is to have one parent and two child components: dashboa ...

Angular Cascade of Multiple Requests

I am currently working on a project where I need to develop a service that can take CSV data, convert it into rows, and then make API requests for each row, adding the formatted results to an output string. To explain further, my code (written in Coffeesc ...

What is the best way to load a partial in Rails asynchronously with AJAX?

I am currently using the following code to load a partial when I reach the bottom of a div containing a table: $(document).ready(function () { $("#pastGigs").scroll(function () { if (isScrollBottom()) { $('#pastGig ...

Securing your Angular2 application with TypeScript for enhanced safety

Looking to create a web application using Angular2 with TypeScript. After researching authentication in Angular2, it seems I need to include the following components: index component (public) login component (public) my private component (private) Thes ...

No responses received for my post on Node Express - a newbie in the world of Node

My current task involves utilizing an Axios post function to send multipart form data to a Node.js Express endpoint using Multiparty for input field processing. Upon receiving the data, the endpoint saves it in the database. I am looking to utilize the re ...

call a custom form submission using jquery first, followed by a regular form submission

Is it possible to attach a submit() handler to a form in order to execute an ajax request, and then have the form submit itself normally once the request is complete? Using $('#myForm').submit() seems to just result in the same function being cal ...

Enhance the axis functionality in c3.js

My goal is to display a user's financial data for the previous week, but sometimes they may not have data for all seven days. When using c3.js, I noticed that it automatically extends the 'endpoint-value' to the end of the axis. Is there a w ...

Is it secure to use console.time() in a Node.js environment?

Looking at a small snippet of node.js code, here's what I have: console.time("queryTime"); doAsyncIOBoundThing(function(err, results) { console.timeEnd("queryTime"); // Process the results... }); Running this on my development system gives m ...

What is preventing me from successfully sending form data using axios?

I've encountered an issue while consuming an API that requires a filter series to be sent within a formData. When I test it with Postman, everything works smoothly. I also tried using other libraries and had no problems. However, when attempting to do ...