Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140248

Reading in view return values ​from the controller AJAX / ASP.NET UPDATE

$
0
0
  • called by selectbox go into function 'getDepAndMan()',

  • there is a value taken from the selectbox (works)

  • calls functions in the controller 'GetDepartmentAndManager' (works)

  • controller returns value (works)

    {Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<<>f__AnonymousType6<'string, string>>}

    Result View: [0] { UserDepartament = "there is value here / string", UserManager = "there is value here / string" }

  • should go back to ajax and call 'succes: function (employee data)' (works)

  • should assign values ​​to the fields (doesn't work)

  • show an alert (work)

  • show alert with values (doesn't work, show an alert with: undefined undefined)

View:

@(Html
     .DevExtreme()
     .SelectBox()
     .DataSource(d => d
          .Mvc()
      )
     .OnValueChanged("getDepAndMan")
  )
 @(Html.DevExtreme().TextBox()
      .ID("Id_department")
      .ReadOnly(true)
  )
 @(Html.DevExtreme().TextBox()
      .ID("Id_manager")
      .ReadOnly(true)
  )

<script type="text/javascript">

    function getDepAndMan() {


        var userId = {
           nazwaValueId: $("#idName").dxSelectBox("instance").option("value")
        };

        $.ajax({

            url: "@Url.Action("GetDepartmentAndManager", "Uzytkownicy")",
            type: "POST",
            dataType: "json",
            data: {"userId": JSON.stringify(userId)},
            cache: false,
            success: function (danePracownika) {
                  $("#Id_department")
                     .dxTextBox("instance")
                     .option("value", danePracownika.UserDepartament);
                 $("#Id_manager")
                     .dxTextBox("instance")
                    .option("value", danePracownika.UserManager);

                alert(danePracownika.UserDepartament + "" + danePracownika.UserManager);
            },
            failure: function (error) {
                alert(error);

            },
            error: function (error) {
                alert(error);
            }
        });
    }
</script>


Controller:

[HttpPost]
public ActionResult GetDepartmentAndManager(string userId) 
{

  dynamic serializer = JsonConvert.DeserializeObject<IDictionary>(userId);

            var IdCzlowieka = serializer["nazwaValueId"];

            int IntIdCzlowieka = Convert.ToInt32(IdCzlowieka);

            var danePracownika = _uzytkownicyContext.Uzytkownicy.Where(x => x.Id == IntIdCzlowieka).Select(s => new
            {
                UserDepartament = s.Departament,
                UserManager = s.ManagerLogin

            });


   return Json(danePracownika);
}

return : //

[0] { UserDepartament = "there is value here / string", UserManager = "there is value here / string" }

EDIT

The question is, what's wrong with the code, why it doesn't work for me?

.


Viewing all articles
Browse latest Browse all 140248

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>