【问题标题】:Convert Json results into HTML form using ASP.NET Core 5 MVC使用 ASP.NET Core 5 MVC 将 Json 结果转换为 HTML 表单
【发布时间】:2021-01-16 12:12:07
【问题描述】:

我想在从下拉选择列表中选择 ID 时显示与特定卡相关的余额,并在 [在此处输入图像描述][1]在表单上输入显示余额值。

但我得到的只是运行程序时的 JSON 结果。我的表单没有出现

控制器中的测试动作:

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

Index 视图(测试)

@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>

结果是这样的

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

【问题讨论】:

    标签: json forms asp.net-core-mvc dropdown asp.net-core-5.0


    【解决方案1】:

    从您的代码中不清楚,因为 ajax url 是剃刀页面路由而不是 mvc。你都启用了吗?

    此外,从您之前的帖子中,我看到 View 名称是 Test.cshtml,而 Test 操作返回 Json,因此当您访问 /Custom/Test 时,您只会得到 Json 结果而不是 View。

    如果使用Test动作显示html视图,我认为应该返回View:

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

    更新:

    有一个动作(测试)来返回视图(Test.cshtml)。只是 reutn View() 没有任何数据,因为我没有看到您在视图中使用模型(VmIstanbul)值的任何地方。

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

    有另一个动作将数据返回给 ajax,给它一个除Test 之外的名称。例如:

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

    并将ajax url改为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>
    }
    

    结果:

    【讨论】:

    • 我试图将返回控制器转换为视图,但我在选择和输入 HTML 时得到(未定义)你知道我该如何解决它吗?
    • 嗨@ALAAELDIN,这是未定义的,你能告诉我们详细的错误消息吗?另外,如果您可以分享SelectAll方法的代码代码,可能会有所帮助。
    • 嗨@mj1313 我想以视频的形式与你分享这个问题,但我不知道如何上传,所以你可以点击(这是我的问题)并查看它,我做了作为 gif 照片!here is my problem.
    • 嗨@ALAAELDIN,我无法访问你的链接,我觉得在github上分享你的项目更方便。
    • 嗨@mj1313 你能再试一次吗?我已经给你访问权限了吗? !here is my problem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多