Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
Asp.Net Core - Return values from the controller and put the values ​into the inputs by Ajax?

I have a drop down list and I want the value to be sent to the controller when an option is selected,replaceing the returned values ​​in the desired inputs

Html Inputs :

<input type="text" class="form-control js-inputs" id="microchipcode">
<input class="form-control js-inputs" id="fa-horse">
<input type="text" id="fa-fatherhorse" class="form-control js-inputs">

Html DropDown:

$('.js-selected-item').change(function () {               
let Value = $(this).val();
$.ajax({
    data: { value: Value },
    Url: "/Horse/GetHorseByMcode",
    type: "post",
    success: function (data) {
    }
});              

});

Controller :

 public async Task<IActionResult> GetInfoHorse(string value)
    {
        var horse = await _coach.GetHorseByMcode(value);
        if (horse != null)
        {
            return Json(horse);
        }

        return NotFound();
    }

Query :

public async Task<Horse> GetHorseByMcode(string value)
    {
        return await _context.Horses.SingleAsync(h => h.MicrochipCode == value.Trim());
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to put a value into an input via js, add an ID to the inputs and do the following:

JS:

document.getElementById('//inputId').value = '';

Jquery:

("#//inputId").val("");
about 4 years ago · Juan Pablo Isaza Report

0

How do I access the data inside the object?

You can check the request and response in f12 developer tool Network tab, like below.

enter image description here

and implement frontend code to populate input field(s) in ajax success callback function based on your actual returned data.

For example:

success: function (data) {
    $("#fa-fatherhorse").val(data.fatherHorse);
    //populate other inputs based on your actual returned data

How to replace Drop Down list ?

If you want to dynamically set the selected value/option of the dropdown, you can try:

$("#dropdown_id_here").val(value_of_selected_option);

If you want to dynamically append <option> to your dropdown, you can try:

var newoption = "<option value='" + option_val + "'>" + option_text + "</option>";

$("#dropdown_id_here").append(newoption);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!