Having difficulty inserting text into an input field using the Json value as a reference

I am currently facing an issue with the script in my View:-

<script type="text/javascript">

    $(document).ready(function () {
        $("#Server_VirtualCenterID").attr("disabled", "disabled");
        if ($(this).val() == "3") {
            $("#Server_VirtualCenterID").removeAttr("disabled");
        }
        else {
            $("#Server_VirtualCenterID").attr("disabled", "disabled");
        }
        $("#Switch_RackID").change(function () {
            var idRack = $(this).val();
            $.getJSON("/Rack/LoadDataCenterByRack", { id: idRack },
            function (RackData) {
                var select = $("#Switch_TMSRack_DataCenter_Name");
                select.empty();
                select.append(Text);
                });
            });
        });

</script>

This script is supposed to trigger an action method when a dropdown list is changed as shown below:-

public JsonResult LoadDataCenterByRack(int id)
    {
      string datacentername = repository.FindRack(id).DataCenter.Name;

      var DCData = new { Text = datacentername, Value = datacentername };

        return Json(DCData, JsonRequestBehavior.AllowGet);
    }

However, I am encountering an issue where the returned JSON data is not being added as text for the Switch_TMSRack_DataCenter_Name input field. Can someone provide guidance on how to fix this?

Answer №1

To properly retrieve the data, it is recommended that you utilize select.append(RackData.Text) rather than select.append(Text)

Assuming that RackData is in JSON format and serves as the response data, you must access the Text property within RackData.

For a simpler approach, I recommend using:

$("#Switch_TMSRack_DataCenter_Name").val(RackData.Text);

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

When two divs are activated, the third div goes into invisible mode

I attempted this code, but it doesn't appear to be functioning correctly. if(div1)&& (div2)==display:none { div3=display:none; } else { div3=display:block; } ...

Steps to transform a datatable into a JSON object

Here is the data structure of my datatable: MCuserid Firsatname address1 address2 city educationinfo institute degree 1760 Rickert vbn addresstwo hyderabad Second College Second Degree 1766 Abhinav jhgjkhk testa ...

Angular2 Error: Cannot have two identifiers with the same name, 'PropertyKey' is duplicated

I am currently developing an application with angular2 using angular-cli. Unfortunately, angular-in-memory-web-api was not included by default. After some research, I manually added the line "angular-in-memory-web-api": "~0.1.5" to my ...

Display the properties of the nested object

I am trying to extract and print the postal_code value from the JSON file provided below: { "results" : [ { "address_components" : [ { "long_name" : "286", "short_name" : "286", "t ...

Save the current application state as either a JSON or TSV file within the backend

While many React tutorials on surveys/forms focus on front-end mechanics, I find myself in a different boat. My front-end is pretty much where I want it to be, but I have realized that my knowledge of back-end programming is virtually nonexistent. I decid ...

What benefits does JavaScript offer with the strategy of storing functions within variables?

Lately I've come across some code where functions are being stored inside variables and then called in the typical way. For example: var myFunctionName = function() { Code Here... } myFunctionName(); I'm aware that there may be numerous b ...

Customize the default styles for Angular 2/4 Material's "md-menu" component

Seeking to customize default styles of md-menu in Angular Material. The challenge lies in the dynamic generation of elements by Angular Material, preventing direct access from HTML. Visual representation of DOM: https://i.sstatic.net/v8GE0.png Component ...

Utilize the app variable within the file that is required by the Node application

Recently, I've set up the following directory structure: - build/ -- app.js -- middlware/ --- authorization.js -- model --- Model1.js --- Model2.js -- routes --- common.js As it's just the beginning of my project and more files will be added, I ...

Issue with ng-repeat directive not functioning

Let's dive into this directive: .directive('img', function () { return { restrict: 'E', link: function (scope, elem, attr) { if (attr.src && attr.type === "extension"){ var ...

I possess a JSON file containing a variety of values. I am seeking a method to allocate distinct colors to different value ranges based on their value. How can I achieve this?

I have a series of values that need to be color-coded based on ranges "fsr": [148.0, 136.0, 146.0, 138.0, 158.0, 144.0, 142.0, 146.0, 140.0, 156.0, 140.0, 148.0, 154.0, 154.0, 154.0, 142.0, 146.0, 166.0, 154.0, 144.0, 142.0, 138.0, 140.0, 154.0, ...

Manipulating the data stored in a JSON file with MFC

While I can successfully parse JSON files in MFC, I am encountering difficulties when it comes to modifying the values. Is there a simpler method for adding new values without having to convert them to native file types, make changes, and then convert back ...

Encountering issues with `this.props.navigation` being undefined when utilized in App.js within the Stack Screen

When I click on the bag icon in the header, I need to navigate to another screen. However, I am encountering an error: undefined is not an object (evaluating '_this.props.navigation') The issue seems to be related to using it inside a Stack.S ...

Trouble with React Material Modal TransitionProps triggering onEntering event

Currently, I am in the process of updating Material UI to version 5. Initially, I encountered an error stating that onEntering is deprecated and should be replaced with transitionprops. There is a specific method (let's name it doSomething) that I wa ...

What is the best way to make a JavaScript button change its class on the second click, either on itself or on other buttons with the same class?

I am trying my best, with my limited knowledge of Javascript, to write plain Javascript for some buttons to achieve the following: a) Click any button, like button1, and its content will be shown b) Click the same button again, and its content will be hid ...

Expanding the HTML form dynamically with Javascript to include a new row of form inputs

I am currently working on dynamically adding a new row of HTML form inputs to an existing "View" HTML form using Javascript. This new row includes 6 inputs - Type (dropdown), Length, Width, Height, Weight, and Quantity. The data from these inputs is access ...

The perplexing problem of scrolling in Dojox/mobile/scrollableview

I've noticed some scroll issues with the dojox/mobile/ScrollableView (version 1.10+) in my cordova application, specifically on Android phones. This problem seems to be affecting other widget-based architectures and even some store apps as well. I wou ...

Determine the specific line that was clicked during the execution of the PHP loop

In my PHP script, I have a while loop that fetches data from a MySQL database. This loop generates multiple unordered lists dynamically displaying the rows retrieved from the database. Each row includes a button and a hidden input containing a link as it ...

What is the best way to save the values of all the input fields in a datatable, excluding any fields that are empty?

$('form').on('submit', function(e){ var form = this; // Encoding a set of form elements from all pages as an array of names and values var params = table.$('input').serializeArray(); // Iterating over all form ...

Using jQuery to replace the value of a button with a variable string that has been concatenated

I am facing a dilemma with a button on my webpage. The value displayed on the button is a combination of variables and strings, like so: $('#btn1').attr('value','question'+currid+'ans1').button('refresh'); ...

Using the Neo4j REST API to generate a Node containing an integer property

Currently, I am developing a Neo4j REST API wrapper for nodejs (node-neo4j) in preparation for v2.0 of Neo4j. You can check out my version at: https://github.com/Stofkn/node-neo4j (forked from https://github.com/philippkueng/node-neo4j) I'm wonderin ...