【问题标题】:Display Information from LINQ query显示来自 LINQ 查询的信息
【发布时间】:2020-12-09 20:21:26
【问题描述】:

所以我有一个从 JS 传递到我的控制器的字符串,如下所示:

JavaScript

function findEmployees(userCounty) {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: '@Url.Action("getCounty", "Contact")',
        data: JSON.stringify(userCounty),
        contentType: "application/json",
    });
}

控制器

    [HttpPost]
    public ActionResult Index([FromBody] string userCounty)
    {
        var county = userCounty.Substring(0, userCounty.IndexOf(" "));
        var query = from m in _context.model where m.county == county select new Model 
        {
          FirstName = m.Firstname
          LastName = m.LastName
        };

        if (query == null)
        {
            return NotFound();
        }
        return View(query.ToList());
    }
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

查看

@model Project.Models.ModelName
<table class="table">
<tbody>
    <tr>
        <td>
            @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)
        </td>
    </tr>
</tbody>

我可以将字符串从 JS 传递到我的控制器并查询数据库,但是如何更新页面以在我的视图中显示查询结果?任何事情都有帮助。谢谢!

【问题讨论】:

    标签: javascript c# asp.net asp.net-mvc asp.net-core


    【解决方案1】:

    ajax返回的数据是text或者json。如果你想使用 c# 来更新页面。可以让actiongetCounty返回局部视图,局部视图自动返回html数据。

    更改操作getCounty

        [HttpPost("getCounty")]
        public ActionResult Index([FromBody] string userCounty)
        {
            var county = userCounty.Substring(0, userCounty.IndexOf(" "));
            //...
            return PartialView(query.ToList());
        }
    

    部分视图 Index.cshtml

    @model List<ModelName>
    <table class="table">
    <tbody>
        @for (var i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(model => model[i].FirstName) @Html.DisplayFor(model => model[i].LastName)
                </td>
            </tr>
        }
    </tbody>
    </table>
    

    查看

    @model ModelName
    <div id="datalist">
        
    </div>
    <!--other code-->
    @section Scripts{
        <script>
        function findEmployees(userCounty) {
            $.ajax({
                type: "POST",
                //dataType: "json",
                url: '@Url.Action("getCounty", "Contact")',
                data: JSON.stringify(userCounty),
                contentType: "application/json",
                success: function (data) {
                    $('#datalist').html(data)
                },
                error: function (e) {
                    console.log(e)
                }
            });
        }
        </script>
    }
    

    可以根据userCounty生成不同的数据表

    【讨论】:

    • 非常感谢!
    • 那么我需要为我的局部视图创建一个新文件吗?很抱歉我对部分观点非常不熟悉。编辑:我也收到一条错误消息,告诉我“(model => model[i].FirstName)”未设置为对象的实例。这是控制器的问题吗?
    • 致读到此文的其他人:通过另一个问题参与此项目后,我怀疑该案例是经典的 X/Y。 OP 开始使用 Ajax 发布数据,而他应该一直使用纯格式。因此,解决一个问题会导致其他问题,直到应用程序准备好 StackOverflow 方式。
    • 是的,需要根据局部视图生成一个新文件。对于这个错误,请检查@model List&lt;ModelName&gt;的引用是否正确。这是部分视图的document
    【解决方案2】:

    您可以像这样将列表添加到页面。然后您可以在每个循环中按 div 或 ul 列表。

    function findEmployees(userCounty) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: '@Url.Action("getCounty", "Contact")',
                data: JSON.stringify(userCounty),
                contentType: "application/json",
                success: function (result) {
                    if (result.data.length !== 0) {
                        $.each(result.data, function (index, value) {
                            var firstName = value.firstName;
                            var lastName = value.lastName;
                        });
                    }
                },
            });
        }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多