Click here to Skip to main content
15,884,973 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))



C#
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
            {
                i.Id,
                i.Nazwa,
                i.Departament,
                i.Login
            });

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

        [HttpGet]
        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**
HTML
``` 
                          @(Html
                           .DevExtreme()
                           .SelectBox()
                           .ID("Id_name")
                           .DataSource(d => d
                               .Mvc()
                               .Controller("Uzytkownicy")
                               .Key("Id")
                               .LoadAction("GetItems")
                           )
                           .DisplayExpr("Nazwa")
                           .ValueExpr("Id")
                           .SearchEnabled(true)
                           .OnValueChanged("getDepAndMan")
                        )
                        @(Html.DevExtreme().TextBox()
                       .ID("Id_department"))

                        @(Html.DevExtreme().TextBox()
                           .ID("Id_manager"))


<script type="text/javascript">

    function getDepAndMan() {

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

        $.ajax({
             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");
             }
         });
    }
</script>


**Do not work:**
The code does not throw an error, but does not enter the downloaded data into text boxes
Posted
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:
JavaScript
success: function(data) {
    console.debug(data);
    /*
    Expected:
    { UserDepartment: "...", UserManager: "..." }
    
    Actual:
    [
        { UserDepartment: "...", UserManager: "..." }
    ]
    */
To fix that, you need to change your action so that it only returns a single record:
C#
[HttpGet]
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
 
v2
Comments
Member 14713380 29-Jan-20 9:23am    
Name "s" dont exist in current context
Richard Deeming 29-Jan-20 9:30am    
Fixed:
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