显示来自 LINQ 查询的信息

所以我有一个字符串从 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 传递到我的控制器并查询数据库,但如何更新页面以在我的视图中显示查询结果?任何事情都有帮助。谢谢你!


HUH函数
浏览 145回答 2
2回答

慕工程0101907

ajax返回的数据是text或者json。如果你想用c#来更新页面。你可以让actiongetCounty返回partial view,partial view自动用html返回数据。改变行动getCounty。&nbsp; &nbsp; [HttpPost("getCounty")]&nbsp; &nbsp; public ActionResult Index([FromBody] string userCounty)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var county = userCounty.Substring(0, userCounty.IndexOf(" "));&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; return PartialView(query.ToList());&nbsp; &nbsp; }PartialView索引.cshtml@model List<ModelName><table class="table"><tbody>&nbsp; &nbsp; @for (var i = 0; i < Model.Count; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.DisplayFor(model => model[i].FirstName) @Html.DisplayFor(model => model[i].LastName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; }</tbody></table>看法@model ModelName<div id="datalist">&nbsp; &nbsp;&nbsp;</div><!--other code-->@section Scripts{&nbsp; &nbsp; <script>&nbsp; &nbsp; function findEmployees(userCounty) {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '@Url.Action("getCounty", "Contact")',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: JSON.stringify(userCounty),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: "application/json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#datalist').html(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function (e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; </script>}可以根据userCounty生成不同的数据表

猛跑小猪

您可以像这样将列表获取到页面。然后您可以在每个循环中按 div 或 ul 列表内部。function findEmployees(userCounty) {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '@Url.Action("getCounty", "Contact")',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: JSON.stringify(userCounty),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: "application/json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.data.length !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(result.data, function (index, value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var firstName = value.firstName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lastName = value.lastName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript