Using C# and AJAX to refresh the view with the latest model data

In my .NET web app, I have a process that involves navigating from Page A to Page B where I input a barcode scan result. Page B then displays a table generated by various queries and data inserted into a view model.

What I am seeking is the ability to repeat this process while on Page B. Essentially, when I scan another barcode on Page B, I want it to trigger the same sequence as on Page A: reload Page B with updated results. My approach involves using AJAX to send a parameter to the controller, execute the necessary queries and actions, and return a refreshed View with the updated View Model. However, the issue arises when the page fails to reload with the new data and retains the old information.

I attempted placing the table in a partial view and trying to trigger a reload after each barcode scan on Page B. Unfortunately, this method does not load all the essential JavaScript code embedded within Page B, which is vital for tasks like editing table values. The JavaScript code is integral to Page B, not the separate ".cshtml" partial view.

The AJAX CODE snippet demonstrates how I send an ID to the controller, receive HTML content back, and update a div element containing the partial view of the table:

$.ajax
({
    type: 'POST',
    url: '@Url.Action("QueryBollaTestP", "Bolla")',
    data: JSON.stringify({ 'NumBolla': evt.state.code }),
    contentType: 'application/json; charset=UTF-8',
    dataType: "html",
    success: function (data)
    {
        $(".tesst").html(data);
    },
})

Here's a snippet of the Controller code handling the POST request and returning the updated partial view with the pickingViewModel data for the table:

[HttpPost]
public ActionResult QueryBollaTestP(string NumBolla){
......... perform necessary operations and queries..... populate pickingViewModel for the table
return PartialView("_BollaTable", pickingViewModel);
}

My main objective is to find a solution that ensures the model on Page B reflects the correct data after updates, regardless of whether it involves using a partial view or not. The key requirement is to refresh the page with the accurate information and maintain the execution of the JavaScript code.

Answer №1

Is the only variable being used the bar code (NumBolla)? And what is the purpose of Page A exactly? I'm inclined to believe that a single View should suffice.

public async Task<ActionResult> PageA(int NumBolla){
..perform necessary operations to create model 
return View(model)
}

If you wish to load a new barcode scan using JavaScript: window.location.href = "../PageA/" + NumBolla;

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

Fulfill the promise once all images have finished loading

When I was preloading images, my initial code looked like this: function preLoad() { var deferred = $q.defer(); var imageArray = []; for (var i = 0; i < $scope.abbreviations.length; i++) { imageArray[i] = new Ima ...

How long does a property with just a getter function last?

Suppose I have created an instance of MyClass. Will the Foo property contain a reference or will it be null at this point? What happens after I invoke UseFoo() and exit the scope - is Foo disposed or just foo? public class MyClass { private readonly ...

Create shorter nicknames for lengthy reference names within the ng-repeat loop

Is it possible to assign an alias to a long reference name in ng-repeat? Currently, I have 2 complex objects where one acts as a grouped index for the other. Although the ng-repeat template code is functioning correctly, it's getting hard to read and ...

The issue of nested tabs in Bootstrap 3 causing problems with sliders within the main tab has

Hello, I am utilizing Bootstrap 3 tabs. Within the nav-tabs, there are three tab-panes: the first tab contains text only, the second tab includes a slider, and the third tab has nested tabs. However, I encountered an issue where the slider in the nested t ...

What is the best way to utilize the GET Method with a hashtag incorporated into the URL?

For instance: www.sample.com#?id=10 Currently, I am not able to retrieve any value from $_GET['id']. Despite my attempt to eliminate the hashtag from the URL using JavaScript, no change occurs: $(document).ready(function(){ $(window.location ...

Looking to integrate django-filter using an ajax request

Utilizing django-filter==2.1.0 for my search filtering system has been effective. However, I now face the challenge of integrating an ajax call into the search filter. Below are the snippets of code I am currently working with: views.py def test_view(req ...

The website appears to be loading in a unique way on mobile upon the second loading

While developing my personal website, I encountered a bug that occurs when accessing the site on my Android phone using Firefox or Chrome. The issue arises when the page initially loads correctly, but upon refreshing, the layout is displayed differently. ...

Concerns with updating parts of a webpage using asp.net mvc, jquery,

Struggling with a partial page update issue on Asp.net mvc 3 razor that I need help solving In my _LogOn partial view, there are two text boxes for Email and Password along with a login button: @using (Html.BeginForm()) { @Html.ValidationSummary(tr ...

Steer clear of redundant double-checking in LINQ queries

In my scenario, I am managing a collection called Dictionary<string, bool> where the key represents a control's ID and the value indicates its visibility status: var dic = new Dictionary<string, bool> { { "rowFoo", true}, { "rowBa ...

Encountered an error while running NPM build command: "Warning: The node type RestProperty has been updated to RestElement in Babel 7.0.0"

We have been encountering Trace warnings stating that the node type RestProperty has been renamed to RestElement in our builds. Although the build is successful, we are seeing a lot of these warnings since upgrading to babel 7.0.0 I attempted the solution ...

How to modify a variable in the Config.json using a Discord.js command

Lately, I enhanced my bot's functionality by allowing it to retrieve the color for embeds from a file specified in my config.json. All I need to do is modify something like: "embedcolor": "00A950" to "embedcolor": "0 ...

How to dynamically update form select options using Zend Framework 2 (2.3) and AJAX

I'm facing an issue with concatenating 3 dynamic selects - state, country, city - using an ajax request. It seems more complex without zf2! The function works fine when $idState is manually set within stateCountryCityAction (e.g. $idState = 1;), but d ...

Exploring the depths of databases with SQL Server

I've been making good progress on my website project, but I'm stuck on getting the search feature to work properly. What I want is for it to function like this: On my master page, there's an asp:textbox and an asp:button. When a user enters ...

Angular 14 is experiencing issues with NgRx Store failing to properly recognize the payload

The issue lies in TypeScript not recognizing action.payload.index as a valid property. I am unsure how to resolve this problem and make the 'index' visible in my project. shopping-list.actions.ts import {Action} from "@ngrx/store"; im ...

Navigating to a new page once a backend function in Express has finished executing

Recently, I have been experimenting with express web servers to create a website that allows users to sign in using Discord's OAuth2 API. In order to secure sensitive information, I have been utilizing the express-session npm module to store data with ...

communication pathway with multiple variables

Seeking advice on passing multiple parameters into a Thread.Start routine. Any ideas? I initially thought about extending the class, but unfortunately, the C# Thread class is sealed. Below is my idea of how the code should look: ... Thread standardT ...

Implementing dynamic components in Vuejs by passing props from objects

I have developed a dashboard application that allows users to customize their dashboard by adding widgets in any order. While the functionality is working fine, I am looking to address some technical debt and clean up the code. Let's simplify things ...

Flask Session Persistence Issue: Postman Successful, Javascript Fails

Developing a Flask server to facilitate communication between backend Python functionality and Javascript clients on the web has been my recent project. I am trying to harness Flask's `session` variable to retain user-specific data throughout their in ...

An error occurred as the entry point named 'sqlite3_open_v2' was not found in the DLL 'sqlite3'

Currently, I am developing a Windows Forms application. Upon running the application on Windows 8 using Visual Studio 2012, I encounter an issue where the application runs partially and then crashes. The error message states "{"Unable to load DLL 'sql ...

The length of ASP.NET buttons got reduced

Recently, I noticed that all the buttons in my project, including those in the masterpage and .aspx files, have become shorter by about 25px in length. I haven't made any changes to the .css file, so I'm unsure what could be causing this. I vague ...