【问题标题】:How to filter list according to the value of text box如何根据文本框的值过滤列表
【发布时间】:2017-03-28 07:20:15
【问题描述】:

我是 asp.net 和 c# 的新手。

我需要根据文本框过滤列表并显示在网页中。所以我在控制器中做了以下操作。

 public PartialViewResult GetDetails (int id)
    {
        List<VoInfoDetail> model = db.VoInfoDetails.ToList();
        return PartialView("VoDetails", model);
    }

我在视图中写了下面的 jQuery

function getdetails() {
            var id = $("#VoNo").val();
            $.ajax({
                url: "/VoInfoHeaders/GetDetails",
                type: "GET",
                data: { id: id },
                success: function (data) {
                    $("#VoDetails").html(data);
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText);
                }

            });
        }

如何将参数 (id) 从视图传递到局部视图控制器以根据该值过滤数据。我已经通过了 (id) 但它没有过滤

【问题讨论】:

  • 将方法更改为 public PartialViewResult GetDetails (string id)(或 int id 或任何与您传递的类型相匹配的方法)
  • 我做了并且我编辑了代码但仍然无法正常工作
  • 您没有在where 子句中使用id
  • 什么不起作用?方法中的id 参数将包含您所要求的文本框的值!
  • @StephenMuecke 他实际上是在询问条件在哪里,但他不知道如何申请列表

标签: c# asp.net-mvc view controller partial-views


【解决方案1】:

我已经通过了 (id) 但它没有过滤

因为过滤逻辑要自己写,即:

public PartialViewResult GetDetails (int id)
{
    List<VoInfoDetail> model = db.VoInfoDetails
             .Where(x => x.Id == id)
             .ToList();
    return PartialView("VoDetails", model);
}

【讨论】:

  • 非常感谢……这是我的错。我以为我可以这样写 List model = db.VoInfoDetails.ToList(id);
【解决方案2】:

将 id 作为参数添加到 GetDetails,然后适当地更改您的模型获取

public PartialViewResult GetDetails ( int id )
{
    List<VoInfoDetail> model = db.VoInfoDetails.Where( m => condition testing id here ).ToList();
    return PartialView("VoDetails", model);
}

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多