Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to display the balance related to a specific card when the Id is being selected from the dropdown select list and show the balance value in [enter image description here][1]input on a form.

But what I am getting is only the JSON results when I am running the program. my form is not appearing

The Test action in the controller:

public IActionResult Test()
{
    var data = from als in ctx.TbIstanbulCards select new { als.IstanbulCardId, als.Balance };
    return Json(data.ToList());
}

The Index view (Test)

@model VmIstanbul

@section Scripts{
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>

    <script>
        $(document).ready(function () {
            var options = {};
            options.url = "/Customer?handler=SelectAll";
            options.type = "GET";
            options.dataType = "json";
            options.success = function (data) {
                data.forEach(function (element) {
                    $("#customerid").append("<option>
        "
        + element.IstanbulCardId + "
    </option > ");
            });
            };
            options.error = function () {
                $("#msg").html("Error while
    making Ajax call!");
        };
            $.ajax(options);
        });

        $("#customerid").change(function () {
            var options = {};
            options.url = "/Customer/" +
                $("#customerid").val() + "?handler=SelectByID";
            options.type = "GET";
            options.dataType = "json";
            options.success = function (data) {
                $("#companyname").val(data.Balance);
                //$("#contactname").val(data.contactName);
                //$("#country").val(data.country);
            };
            options.error = function () {
                $("#msg").html("Error while making Ajax call!");
            };
            $.ajax(options);
        });


</script>

}
<form id="myForm" asp-controller="Customer" asp-action="Test">
    <label for="departmentsDropdown"><b>Card</b></label>
    <select class="form-control" onchange="getValue()" id="departmentsDropdown" name="departmentsDropdown"></select><br />
    <input type="text" value="" id="show" />
</form>

The result appear like that

https://i.stack.imgur.com/sqDVl.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

It is not clear from your codes, since the ajax url is razor page route but not mvc. Do you enable both of them?

Besides, from your previous post I see that the View name is Test.cshtml, while the Test action return Json, so when you access /Custom/Test, you just get the Json results instead of a View.

If the Test action is used to display the html view, I think it should return View:

public IActionResult Test()
{
    return View();
}

Update:

Have an action(Test) to return View(Test.cshtml). Just reutn View() without any data since I didn't see any place you use the model(VmIstanbul) value in your view.

public IActionResult Test()
{
    
    return View();
}

Have another action to return data to ajax, Give it a name except Test. For example:

public IActionResult TestSelect()
{
    var data = from als in ctx.TbIstanbulCards select new { als.IstanbulCardId, als.Balance };
    return Json(data.ToList());
}

And change the ajax url to TestSelect

@section Scripts{
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "/Customer/TestSelect",
                success: function (data) {
                    console.log(data);
                    var s = '<option value="-1">Please Select a Card id</option>';
                    for (var i = 0; i < data.length; i++) {
                        s += '<option value="' + data[i].balance + '">' + data[i].istanbulCardId + '</option>';
                    }
                    $("#departmentsDropdown").html(s);
                }
            })
        })

        function getValue() {
            var myVal = $("#departmentsDropdown").val();
            $("#show").val(myVal);
        }
    </script>
}

Result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...