Refresh a div on an ASP MVC page with a controller action without the need to reload the entire page

In my controller, I have implemented the following action:

public ActionResult TestItemLogistic()
    {
        ControlViewModel model = new ControlViewModel();
        model.itemSelected = "Logistic";
        return RedirectToAction("MenuList", model);

    }

When I click on a

<a <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec849e898ad1ac999e80c2ad8f98858382">[email protected]</a>(subItem.action, subItem.controller)>

I want the functionality to be executed without reloading the entire page, simply updating specific div elements.

Do you have any suggestions or ideas?

Thank you.

Answer №1

If you want to achieve this task, consider utilizing Ajax in conjunction with jQuery. You can find detailed information on jQuery ajax by visiting the following link: http://api.jquery.com/jQuery.ajax/

Answer №2

Utilize .load to update the content within the chosen element(s) with the HTML response fetched from a specified URL.

For instance:

<a href="@url.Action(subItem.action, subItem.controller)" id="ClickMe">Click Here</a>
<div id="ReplaceMe"></div>

This can be achieved as follows:

<script type="text/javascript">
 $(function() {
    $('#ClickMe').click(function(e) {
       e.preventDefault();
       $('#ReplaceMe').load($(this).attr('href'));
    });
 });
</script>

Update: To clarify, you are looking for a link to an action that returns a URL, and then load the contents of that URL.

public ActionResult TestItemLogistics()
{
    ControlViewModel model = new ControlViewModel();
    model.selectedItem = "Logistics";

    UrlHelper u = new UrlHelper(ControllerContext.RequestContext);
    return Content(u.Action("MenuList", model));

}


<script type="text/javascript">
 $(function() {
    $('#ClickMe').click(function(e) {
       e.preventDefault();
       $.get($(this).attr('href'), function(url) {
              $('#ReplaceMe').load(url);
       });
    });
 });
</script>

Answer №3

Utilize the Ajax attribute in your view to easily update specific elements by their ID.

@Ajax.RouteLink("TestLink", new { controller = "Person", action = "Partial" },new AjaxOptions() { InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, UpdateTargetId = "ElementID" })

Give this a try!

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

Text that fades in and out based on scrolling past a specific point

There's a text containing <p> and <h1>. The text finishes with one <h1>. I'm looking to speed up the Y translation of the <p> twice when I reach the bottom of the document (where the last h1 is located in the middle of th ...

A simple way to position a button separate from a form

I have a form with an action like the following: <form action="edit.php" method="post"> . . <button class="btn btn-success" name="submit_mult" type="submit">Edit</button> </form> Initially, I used this form for the first button, ...

Shifting the div with a sliding animation!

My webpage features a video background with text overlay, and I am looking to add a button in the center of the page. When users click on this button, I want the current text div to slide up using a CSS transition, revealing another div with the same effec ...

When interacting with Chrome, the div gets automatically blurred upon being clicked

While working on the development of our website, we stumbled upon something peculiar. We have a set of divs displayed on the screen, resembling the layout of Pinterest. Upon clicking any of these divs, the content within that particular div is loaded into ...

Show a loading icon as the synchronous Ajax request is being sent

Seeking a way to show a spinner while making a synchronous Ajax request to fetch data from the server. Acknowledging that asynchronous mode is preferred, but not permitted in this case. Aware that a sync ajax request can cause browser blocking. An attemp ...

Display all pages in the DataTables plugin while utilizing responsive tables and additional features

I am struggling to combine the responsive and fixed header attributes of my current function with the "Show All" list feature, similar to what is demonstrated in this example: https://datatables.net/examples/advanced_init/length_menu.html I need assistanc ...

PHP-MySQL FlipClock: Counting Down in Reverse

I am looking for a solution to implement a reverse counter using Flipclock. The timer should start from a given time (for example, 19:40:46) and count down to 00:00:00. Here is the code: var clock; $(document).ready(function() { // Get the current ...

"Enhancing Shopify experience by seamlessly adding products without the need for page refresh

Welcome to my first post on this platform. I recently implemented a list of products in my Shopify mini cart, and although the products are successfully added to the cart, clicking "add to cart" redirects me to the cart page. Is there a way to add products ...

What is the best way to showcase AJAX data within a complex HTML structure?

I need assistance with displaying the JSON output in my HTML code. My current HTML code is quite complex, so I am unsure how to include this data and repeat the HTML section for every JSON element. Can you provide guidance on how to achieve this? HTML COD ...

Various forms utilizing the same submission function

Is it possible to use the same submit code for multiple forms on a page that contain various products? $('#myform').submit(function() { var queryString = $(this).formSerialize(); alert(queryString); $.ajax ({ t ...

Implementing MySQL in JavaScript code leads to various possibilities

I am working on a web application that requires retrieving values from a MySQL database. The process involves: PHP code generates an HTML page successfully. Clicking a button updates a cookie, which also works. Using the cookie in a MySQL query, which is ...

Tips for organizing an AngularJS bootstrap Modal for a search feature

I need help with integrating AngularJs, bootstrap, and an API to populate a bootstrap modal popover with JSON data upon clicking a search function button. While I have successfully implemented the data flow, I am struggling to trigger the modal using the b ...

I want to use the enter key for posting, but also have the ability to use the shift and enter key to create a

$("#post_text").keydown(function(e){ // Checking if Enter key was pressed without shift key if (e.keyCode == 13) { // Prevent default behavior e.preventDefault(); } if (e.keyCode == 13 && !e.shiftKey) { alert('test'); } }); I a ...

Merging onsubmit attribute with jQuery submit event listener

My website has a form that is automatically generated by older server-side code. Unfortunately, I am unable to make any modifications to this legacy code for various reasons. The form itself is written in straightforward HTML: <form action="#" id="theF ...

The issue of Jquery not functioning properly within Ajax-included HTML documents

I've tried looking at some other posts, but I couldn't understand them. The jQuery works fine when the page is loaded, but when the same elements are loaded from an AJAX script, the jQuery doesn't work. I know I need to do a callback, but ca ...

Having trouble with Ajax POST and CodeIgniter integration?

I have encountered an issue while trying to echo back code received from Ajax. It seems that when I use contentType:false and processData:false, the functionality stops working. Below is the snippet of my ajax code. The url is correct. Interestingly, if I ...

The JavaScript .load() function fails to run

Attempting to create a straightforward Newsfeed page based on user interests. Here is the code I have implemented for this purpose. The issue I'm facing is that while the code works fine on my local server, it encounters problems when running on an on ...

While working with ajax form submission in CodeIgniter, I encountered an issue where I did not receive the expected value in the

For my project, I am using PHP Codeigniter and submitting a form through an Ajax call. However, I am facing an issue where I am not receiving the expected value in the success message. Below is the code snippet: Ajax Call: function addLocation(){ va ...

The form submission feature is malfunctioning due to JavaScript issues

I have forms that allow file uploads (images), and I am trying to restrict the size of those images to 500KB. However, for some reason, the forms are not submitting and I am unsure why. Below is the jQuery code: $('form').on('submit', ...

Alter the background color of a div contingent on the checkbox being checked within

I am in search of a solution to modify the background color of a parent div that contains a checkbox. The condition is that the background color should only change when the 'checked' attribute is present within the tag. HTML <div class="comp ...