Tips for passing a JSON object to an action in C# using @Url.Action

Hello, I am having trouble sending a JSON object to my action and could really use some assistance. The object is located in a different controller than where I'm trying to send it.

var pData = { id: null, StateCity: statecityName, Zip: zipcode, MaxDistance: maxdist, PhyName: phyName, ClickCnt: 0 };

window.location.href = '@Url.Action("Index", "Different", new { @pData=pData})';

However, when I try to access pData, it says it cannot be found.

The action in my DifferentController looks like this:

public ActionResult Index(PData pData )
{
//some work
return view();
}

Answer №1

If you're having trouble sending a Json object, one workaround is to use query strings for page redirection:

var pData = '?StateCity='+ statecityName + '&Zip='+ zipcode + '&MaxDistance=' + maxdist + '&PhyName=' + phyName;
window.location.href = '@Url.Action("Index", "Different")' + pData;

In your controller action method, make sure to include all required parameters instead of a complex object for binding:

[HttpGet]
public ActionResult Index(string stateCity, string zip, int maxDistance, string phyName)
{
    //work with your parameters
    return view();
}

Add or remove parameters as needed in the query string and action method parameter list. This should work seamlessly for you.

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

Error: ECONNREFUSED - The connection was refused for an unknown reason, even though all other tests have

Currently, I am working with node/express/mongo and attempting to execute a test to locate posts with a specific id. Below is the route I am using: app.get('/api/posts/:id', PostController.getPost); This represents the controller: getPost( ...

Addressing the issue of compatibility with iPhone 5 and 5S in both portrait and landscape modes

I made an attempt to conceal certain elements specifically on iPhone 5 & 5S in both portrait and landscape orientations, but unfortunately, it ended up affecting all iPhone devices. Below is the code snippet used: @media only screen and (min-device ...

Can you explain the differences between get and post methods in Express JS?

Can someone help clarify the difference between post and get in expressjs? I've tried changing the get and post methods in the code below but nothing seems to be happening. Any insights would be appreciated. const express = require('express& ...

When you click, transfer a single item from one array to another and modify the text according to the items in the array

How can I dynamically update a shopping cart feature on my website when a user clicks on the "addtocart" button? I want to create functionality where clicking on the button adds the corresponding product to the cart array, and updates the cart amount displ ...

Utilizing JavaScript and its scope to include text within an HTML document

I am currently working on a program that receives input from the user twice, specifically a risk carrier and a sum (although it is just a placeholder for now), groups these two values together, and then repeats the contents in a loop. You can see the progr ...

Running a Socket.IO application on my server: A comprehensive guide

I have been exploring the process of creating a real-time chat application and I came across various solutions using Socket.Io that work perfectly on my local device. However, when it comes to deploying it on my server, I am at a loss. Any advice or sugges ...

Passing a JavaScript variable to a PHP variable

Initially, I attempted to convert JavaScript to PHP, only to realize that it's not possible due to server and client side executions. Now, my goal is to send a single variable. <script type="text/javascript"> function scriptvariable() { ...

How can you automate the execution of unit tests in Angular before pushing changes to Git?

There have been instances in Angular projects where the unit tests are triggered every time a build is carried out, as well as when running the git push command. In case any tests fail during either of these commands, the process halts until all unit tes ...

What could be the reason for my component's difficulty in retrieving data from the reducer?

I am currently working on a React application where users can register themselves as either a business or a regular user. Users have the ability to search for businesses by name. I'm facing an issue while trying to render my search component, as it is ...

Learn the process of storing data using Realm database in JavaScript

Recently, I started working with react-native and decided to incorporate realm database into my application. My goal is to pre-populate the data using a JSON file. Initially, I attempted to achieve this by utilizing the componentDidMount() function along w ...

"Learn how to convert basic input fields into user-friendly Bootstrap input groups using the power of jQuery

Is there a way to use jQuery to convert my basic input field into a bootstrap input-group like the one shown below? This is how I created the input: <div class='input-group date' id='ff'> <input type='text' class= ...

Unable to execute Javascript function within a click event handler

I am encountering an issue with a div that is loaded through ajax. Here is the structure of the div: <div id="container"> <a href="#" class="easyui-linkbutton submit_data">Click here to submit</a> </div> Within the same file c ...

The Redux dispatch function enters a loop when the success flag is set to true within the state

Currently, my Redux state is structured as follows: const initialState = { data: [], loading: false, success: false, error: null, }; I have implemented a CRUD API in the node backend. In my React application, I have two components: The first com ...

Is my Javascript experiencing a shortage of asyncIds? (Encountered RangeError in inspector_async_hook.js)

One issue that I frequently encounter while using async/await is the following error: RangeError: Value undefined out of range for undefined options property undefined at Set.add (<anonymous>) at AsyncHook.init (internal/inspector_async_hook ...

What is the reason behind System.Uri interpreting consecutive slashes as a folder designation?

It has become a habit for me to always add a trailing slash at the end of my folder paths, which seems to be standard practice (for example, Visual Studio macros like $(ProjectDir) always have a trailing slash). Also, when appending a relative path to an e ...

Encountering a bug: "Undefined property" issue when working with Web Sockets in React Native

I'm a beginner in React Native and Java Script development, and I'm facing an issue while trying to retrieve a JSON object and display it on the client side. I keep getting a "cannot read property of undefined" error when using websockets instead ...

Setting up a function in React to utilize an image stored in state

I have a function in my react component for image classification that retrieves the image from the img tag using document.getElementById: const img = document.getElementById('animal_image');. The image uploaded via the file input updates the sta ...

animation glitches in safari and chrome when using jquery

Hey there! I'm currently working on a form with multiple steps, where I animate the step number for each level of registration. The animation functions perfectly in Firefox, but unfortunately, it's not supported in Chrome and Safari due to an iss ...

Ensure that each function is completed before proceeding to the next one

I've encountered an issue with my react app. After a user submits a form, I need a modal to open and the user's response to be stored in state. Then, based on this response, I have to execute some business logic before finally submitting the form ...

The Material UI Drawer stays closed despite the state being set to true

Currently, I am in the process of developing a WebApp utilizing React and Material UI. Despite following numerous tutorials on implementing the Drawer component and poring over the documentation, I am still struggling to grasp its functionality. Even thou ...