【问题标题】:Search and Display Records from Different Tables using one search box使用一个搜索框从不同的表中搜索和显示记录
【发布时间】:2019-10-22 05:44:56
【问题描述】:

我有 4 个具有相似数据字段的 SQL 表。它们分离的原因是因为它们包含来自不同位置的数据。我已经实现了一个能够从这些表中提取和显示记录的搜索框。但是,还有第四个表,其字段完全不同,我尝试在其上实现相同但没有运气。

我尝试在相同的 if 函数中创建一个表,但在不同的 @foreach 中创建一个表,因为它们位于同一个模型类中

public class MemberViewModel
{
    public string Client { get; set; }
    public string PolicyNo { get; set; }
    public Nullable<short> PolicyType { get; set; }
    public string InsurerName { get; set; }
    public Nullable<System.DateTime> RenewalDate { get; set; }
    public string Status { get; set; }
    public string Telephone { get; set; }
}

public class DisplayData
{
    public List<MemberViewModel> Search(List<string> keywords)
    {
        StringBuilder sqlbuilder = new StringBuilder();

        sqlbuilder.Append("  select * from SingleView  where");

        foreach (string item in keywords)
        {
            sqlbuilder.AppendFormat("([Telephone] like '%{0}%' or     [Client] like '%{0}%' or [PolicyNo] like '%{0}%' or [PolicyType] like '%{0}%') and ", item);
        }
        string sql = sqlbuilder.ToString(0, sqlbuilder.Length - 5);

        return QueryList(sql);
    }

这是视图

@if (ViewBag.Message == true)
    {
        <label id="lblMessage" style="color: #6fcdcd;">Enter Phone Number</label>
    }
    else
    {
        if (Model != null)

        {

            if (Model.Count() != 0)

            {
                <div class="container">
                    <table id="example" class="table table-bordered table-hover table-responsive">
                        <thead class="thead-dark">
                            <tr>
                                <th>Client</th>
                                <th>Policy No</th>
                                <th>Policy Type</th>
                                <th>Insurer Name</th>
                                <th>Renewal Date</th>
                                <th>Status</th>
                                <th>Telephone</th>
                            </tr>
                        </thead>

                        @foreach (var item in Model.OrderByDescending(m => m.Client))
                        {

                            if (item.Status == "CURRENT")
                            {
                                @:
                                <tr style="background-color:#13994a  ;text-align:center;font-family:Calibri">
                                    <td>
                                        @Html.DisplayFor(modelitem => item.Client)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelitem => item.PolicyNo)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelitem => item.PolicyType)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelitem => item.InsurerName)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelitem => item.RenewalDate)
                                    </td>

                                    <td>
                                        @Html.DisplayFor(modelitem => item.Status)
                                    </td>

【问题讨论】:

  • 首先你说所有 4 个表都有相同的字段,最后你说的是第 4 个表的字段完全不同。先说清楚,你的期望请提及。
  • 对不起,我的意思是我有 4 个表,三个有相同的字段
  • 您可以创建一个SQL VIEW 并调用store proceduresearch parameter 以执行特定操作,例如AND OR

标签: c# sql-server asp.net-mvc


【解决方案1】:

您可以使用一些具有对象类型的列表,然后根据需要为每个表使用它,例如:

List<object> l = new List<object>();
        l.Add("sssss");
        l.Add(1);
        l.Add(new SomeObject()
        {
            Id = 1,
            LastNane = 23,
            Name = 1,
            RegDate = DateTime.Now
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多