2

Im calling an ajax function in a modal when I press a BTN which calls a Controller action and passes a parameter to controller. I now want to return strings as a Json back to the view and display them in the same Modal. Weirdly it worked on my laptop with dependency "System.Web.MVC" but doesnt seem to work in "Microsoft.AspNetCore.Mvc". When returning the Jsons I fill the inputs in the modal with those json strings but on the PC with asp.net core the Inputs just stay empty where on the laptop they get filled.

Controller return type

       return Json(new { Nachname, Vorname, UserName });

Ajax

$(document).ready(function () {
    $("#btnGet").click(function () {
        $.ajax(
            {
                type: "POST",
        url: "@Url.Action("getName", "Home")",
                data: {
                    UserName: $("#txtName").val()
                },
                success: function (result) {
                    $('#infos').show();
                    $('#txtName').addClass("form-control is-valid");
                    $('#InputFirstName').val(result.Vorname);
                    $('#InputLastName').val(result.Nachname);
                    $('#InputFirstName').show();
                    $('#InputLastName').show();
                    $('#labelInfo').show();
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });

    });
});

`
10
  • Have you looked in the browser console to see if there are any errors? Also if you do console.log(result); what's displayed in the console? Commented Jan 28, 2019 at 8:17
  • For Core MVC you should try this: return new JsonResult(new { Nachname, Vorname, UserName });, because ControllerBase doesn't have Json method. Commented Jan 28, 2019 at 8:19
  • @TetsuyaYamamoto I tried it but seem to have the same issue still. Would I have to change something in the Ajax? Commented Jan 28, 2019 at 8:22
  • @Izzy yes I tried that. I get the right strings ect in the console.log. Just doesnt get displayed in the Modal Commented Jan 28, 2019 at 8:25
  • @MischaMorf Can you update the question and include the string that is displayed in the console please. Commented Jan 28, 2019 at 8:27

2 Answers 2

1

As I mentioned in the comments, your string which is returned to is:

{nachname: "Morf", vorname: "Mischa", userName: "mrfmi"} 

And you're using it as:

$('#InputFirstName').val(result.Vorname);

Here result.Vorname will be undefined so you need to change it as follows:

$('#InputFirstName').val(result.vorname);
...
Sign up to request clarification or add additional context in comments.

Comments

0

Could it be a missing parameter in your AJAX call?

Try adding:

dataType: "json"

to your parameters.

2 Comments

Thats the string I get when I console.log(result) {nachname: "Morf", vorname: "Mischa", userName: "mrfmi"}
didnt work. Didnt have it in ajax but adding didnt help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.