为了账号安全,请及时绑定邮箱和手机立即绑定

显示来自 LINQ 查询的信息

显示来自 LINQ 查询的信息

HUH函数 2023-11-02 21:55:25
所以我有一个字符串从 JS 传递到我的控制器,如下所示:JavaScriptfunction 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 传递到我的控制器并查询数据库,但如何更新页面以在我的视图中显示查询结果?任何事情都有帮助。谢谢你!
查看完整描述

2 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

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


改变行动getCounty。


    [HttpPost("getCounty")]

    public ActionResult Index([FromBody] string userCounty)

    {

        var county = userCounty.Substring(0, userCounty.IndexOf(" "));

        //...

        return PartialView(query.ToList());

    }

PartialView索引.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生成不同的数据表


查看完整回答
反对 回复 2023-11-02
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

您可以像这样将列表获取到页面。然后您可以在每个循环中按 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;

                    });

                }

            },

        });

    }


查看完整回答
反对 回复 2023-11-02
  • 2 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信