Click here to Skip to main content
15,883,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
**Description of the situation:**

I have selectbox, I choose a person there (there, I download the entire list of employees from the database). I have two more fields (I would like to enter values ​​from other columns (same row) (data of this employee))

using AppEcp.Models;
using DevExtreme.AspNet.Data;
using DevExtreme.AspNet.Mvc;
using Microsoft.AspNetCore.Mvc;
using System.Linq;

namespace AppEcp.Controllers
    public class UzytkownicyController : Controller
        private readonly UzytkownicyDbContext _uzytkownicyContext;

        public UzytkownicyController(UzytkownicyDbContext uzytkownicyContext)
            _uzytkownicyContext = uzytkownicyContext;

          public IActionResult GetItems(DataSourceLoadOptions loadOptions)
            var GetMethod = _uzytkownicyContext.Uzytkownicy.Where(i => i.Firma == "Pekao Leasing").Select(i => new

            return Json(DataSourceLoader.Load(GetMethod, loadOptions));

        public ActionResult GetDepartmentAndManager(string nazwaValue)
            var danePracownika = _uzytkownicyContext.Uzytkownicy.Where(x => x.Nazwa == nazwaValue).Select(s => new 
                UserDepartament = s.Departament,
                UserManager = s.Manager

            return Json(danePracownika);

What I have tried:

**I tried:**

**cshtml page**
                           .DataSource(d => d


<script type="text/javascript">

    function getDepAndMan() {

        var nazwaValue = document.getElementById("Id_name").value;

             type: "GET",
             url: '@Url.Action("GetDepartmentAndManager", "Uzytkownicy")',
             contentType: 'application/json; charset=utf-8',
             data: { nazwaValue : nazwaValue},
             dataType: "json",
             success: function (data) {
                document.getElementById("Id_department").value = (data.UserDepartament);
                document.getElementById("Id_manager").value = (data.UserManager);
             error: function () {
                alert("bad code noob");

**Do not work:**
The code does not throw an error, but does not enter the downloaded data into text boxes
Updated 28-Jan-20 7:21am

1 solution

You need to check your browser's developer tools for errors, and inspect the network request to see what data is sent to the server, and what response comes back.

In this case, I suspect you're getting an array of objects back, where your code is expecting a single object:
success: function(data) {
    { UserDepartment: "...", UserManager: "..." }
        { UserDepartment: "...", UserManager: "..." }
To fix that, you need to change your action so that it only returns a single record:
public IActionResult GetDepartmentAndManager(string nazwaValue)
    // Find the first matching record:
    var danePracownika = _uzytkownicyContext.Uzytkownicy.FirstOrDefault(x => x.Nazwa == nazwaValue);
    // If there is no matching record, return a 404 error:
    if (danePracownika is null) return NotFound();
    // Otherwise, return the details of the record:
    return Json(new
        UserDepartament = danePracownika.Departament,
        UserManager = danePracownika.Manager
Share this answer
Member 14713380 29-Jan-20 9:23am    
Name "s" dont exist in current context
Richard Deeming 29-Jan-20 9:30am    
return Json(new
    UserDepartament = danePracownika.Departament,
    UserManager = danePracownika.Manager

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900