简体   繁体   English

在同一个视图中根据下拉选择MVC C# 将列表返回到表格形式

[英]Return a list into a table form based on dropdown selection MVC C# in the same view

My List that should return a table based on what the ID in the dropdown selected was.我的列表应该根据所选下拉列表中的 ID 返回一个表。

Controller: Controller:

    public List<tblResults> GetResult(int Id)
    {            
            var v = (from a in entities.tblResults
                     where
                     a.result_id == Id
                     select a);
            return v.ToList();                      
    }

My view that is the issue currently.我的观点是目前的问题。

      @if (ViewBag.Result != null)
      {
          @Html.DropDownListFor(m => m.maindropdown, new SelectList(ViewBag.ResultGroup, "id", "mainvalues"), "Please Select", new { @class = "form-control", @id = "dropdown" })


                        }
                        <div id="showorhide"> 
                      <table class="table table-striped" style="width:85%" align="center"> 
                         <tr>                            
                            @foreach (var item in ViewBag.GetResult)
                            {
                        <td>
                            @Html.DisplayFor(modelItem => item.result_id)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.name)
                        </td>
                        </tr></table>
                            }
                    </div>

JS code that trigger the table:触发表格的JS代码:

    $("#dropdown").change(function () {            
        if ($(this).val() == "Please Select") {
            $("#showorhide").hide();
        }
        else {
            var selectedValue = this.value;
            $.get('@Url.Action("GetResult", "Home")', { id: selectedValue }, function (result) {
                if (result) {
                    $("#showorhide").html(result);
                }
            });
        } 

You send your ajax request to GetProduct but your action is GetResult so you send your data wrong place.您将 ajax 请求发送到GetProduct ,但您的操作是GetResult ,因此您将数据发送到错误的位置。 I tidy up your code little bit.我稍微整理一下你的代码。 Your data is not coming because ViewBag.GetResult is empty and you try to append json result as html.您的数据没有出现,因为ViewBag.GetResult为空,您尝试 append json 结果为 html。 So you need to get result and append as table element every result item.因此,您需要获取结果和 append 作为每个结果项的表格元素。 And make sure your GetResult returning some values.并确保您的GetResult返回一些值。

view:看法:

@if (ViewBag.Result != null)
{
    @Html.DropDownListFor(m => m.maindropdown, new SelectList(ViewBag.ResultGroup, "id", "mainvalues"), "Please Select", new{ @class = "form-control", @id = "dropdown" })
}
<div id="showorhide">
    <table class="table table-striped" style="width:85%" align="center" id="resultTable">
        @foreach (var item in ViewBag.GetResult)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.result_id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
        </tr>
        }
    </table>
</div>

js: js:

$("#dropdown").change(function () {
    if ($(this).val() == "Please Select") {
        $("#showorhide").hide();
    }
    else {
        var selectedValue = this.value;
        $.get('@Url.Action("GetResult", "Home")', { id: selectedValue }, function (result) {
            if (result) {
                for (let index = 0; index < result.length; index++) {
                    const e = result[index];
                    $("#resultTable").append("<tr><td>" + e.result_id + "</td><td>" + e.name + "</td></tr>");
                }
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM