0

我正在尝试创建一个搜索功能,允许用户搜索由下拉列表指定的列,我已经能够使搜索工作,但只能通过使用特定列和 contains 方法。任何人都可以帮忙吗?

下面我包含了我的控制器和我的视图,如果您需要任何其他代码,请告诉我。感谢您的时间和帮助!

我几乎肯定这是我必须更改以使用下拉列表而不是特定列进行搜索的代码。

if (!String.IsNullOrEmpty(searchString))
            {
                IAMP = IAMP.Where(p => p.PA.ToUpper().Contains(searchString.ToUpper())); 
            }

控制器

 public class PaController : Controller
    {
        PaEntities db = new PaEntities();

        // Index Method 
        public ViewResult Index(string CurrentField, string sortField, string sortOrder, string currentFilter, string searchString, int? page, string Versions, string VPS, string Directors, string IAMP_SEARCH)
        {
            ViewBag.CurrentOrder = sortOrder = String.IsNullOrEmpty(sortOrder) ? "asc" : sortOrder; // Provides the order to sort
            ViewBag.CurrentField = sortField = String.IsNullOrEmpty(sortField) ? "IAMP_PK" : sortField; // Provides the field to sort

            var IAMP = from p in db.iamp_mapping select p;
            var ISER = from s in db.iamp_search_string select s;

            if (Versions != null && Versions != String.Empty) // Version Dropdown Sort Function     
            {
                IAMP = IAMP.Where(v => v.VERSION.Contains(Versions));
            }

            var VIAMP = from x in db.version_number select x;
            var VPIAMP = from v in db.vp_table select v;
            var DIAMP = from d in db.director_table select d;

            ViewData["SelectedVersion"] = Versions;
            ViewData["Versions"] = new SelectList(VIAMP.Select(x => x.VERSION));
            ViewData["VPS"] = new SelectList(VPIAMP.Select(x => x.VP));
            ViewData["Directors"] = new SelectList(DIAMP.Select(x => x.DIRECTOR));
            ViewData["currentFilter"] = currentFilter;
            ViewData["IAMP_SEARCH"] = IAMP_SEARCH;
            ViewData["IAMP_SEARCH"] = new SelectList(ISER.Select(x => x.IAMP_SEARCH));


            if (Request.HttpMethod == "GET") {

                searchString = currentFilter; //sets the currentFilter equal to Searchstring
                IAMP_SEARCH = sortField;
            }
            else {
                page = 1;                   // defaults to page 1
            }
            ViewBag.CurrentFilter = searchString; // Provides the view with the current filter string

            if (!String.IsNullOrEmpty(searchString))
            {
                IAMP = IAMP.Where(p => p.PA.ToUpper().Contains(searchString.ToUpper())); 
            }

            switch (sortField) {

                default: IAMP = sortOrder == "asc" ? IAMP.OrderBy(p => p.PA) : IAMP.OrderByDescending(p => p.PA); break;
                case "VERSION": IAMP = sortOrder == "asc"? IAMP.OrderBy(p => p.VERSION) : IAMP.OrderByDescending(p => p.VERSION); break;
                case "MAJOR_PROGRAM": IAMP = sortOrder == "asc" ? IAMP.OrderBy(p => p.MAJOR_PROGRAM) : IAMP.OrderByDescending(p => p.MAJOR_PROGRAM); break;
                case "INVESTMENT_AREA": IAMP = sortOrder == "asc" ? IAMP.OrderBy(p => p.INVESTMENT_AREA) : IAMP.OrderByDescending(p => p.INVESTMENT_AREA); break;
                case "VP": IAMP = sortOrder == "asc" ? IAMP.OrderBy(p => p.VP) : IAMP.OrderByDescending(p => p.VP); break;
                case "DIRECTOR": IAMP = sortOrder == "asc" ? IAMP.OrderBy(p => p.DIRECTOR) : IAMP.OrderByDescending(p => p.DIRECTOR); break;
            }


            int pageSize = 15; // number of records shown on page view
            int pageNumber = (page ?? 1); // start page number



            return View(IAMP.ToPagedList(pageNumber, pageSize)); // uses pagedList method to return correct page values
        }

看法

@model PagedList.IPagedList<DBFirstMVC.Models.iamp_mapping>


@{
    ViewBag.Title = "Index";

}

@using PagedList;


<h2 class="corporate sifr">PA Mapping</h2>



@using (Html.BeginForm())
{
    <p>
        Show Version: @Html.DropDownList("Versions","All")
        <input type = "submit" value = "Go" />
    </p>

}

@using (Html.BeginForm())
{
    <div class="lefty">
        Find by @Html.DropDownList("IAMP_SEARCH","All"): @Html.TextBox("SearchString")
        <input type = "submit" value = "Search" />
    </div>

}

<div class="righty">
    @Html.ActionLink("Add a new PA to the database", "Create")
</div>
<br /><br />

<div>
    <div class="lefty">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion   })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion  })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
    Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount
    </div>
</div>


<table>
    <tr>
        <th>
            @Html.ActionLink("PA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "PA", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "PA"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc"/>}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "PA"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc"/>}                                                                                           
        </th>
        <th>        
            @Html.ActionLink("MAJOR PROGRAM", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "MAJOR_PROGRAM", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "MAJOR_PROGRAM"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "MAJOR_PROGRAM"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc"/>}           
        </th>
        <th>
            @Html.ActionLink("INVESTMENT AREA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "INVESTMENT_AREA", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "INVESTMENT_AREA"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc"/>}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "INVESTMENT_AREA"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc" />}   
        </th>
        <th>
            @Html.ActionLink("Version", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VERSION", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VERSION"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VERSION"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc"/>}   
        </th>
        <th>
            @Html.ActionLink("VP", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VP", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VP"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc"/>}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VP"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc"/>}   
        </th>
        <th>
            @Html.ActionLink("DIRECTOR", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "DIRECTOR", Versions = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "DIRECTOR"){ <img src="../../Images/ico_tablesortoffset_asc.gif" alt = "table sort arrow asc"/>}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "DIRECTOR"){ <img src="../../Images/ico_tablesortoffset_desc.gif" alt = "table sort arrow desc"/>}   
        </th>       
        <th></th>
    </tr>
@{
    var row_class = "odd";
}

@foreach (var item in Model) {
     row_class = row_class == "odd"? "even" : "odd";    
    <tr class="@row_class">
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VERSION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DIRECTOR)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PA}) |

            @Html.ActionLink("Delete", "Delete", new { id = item.PA})
        </td>
    </tr>
}

</table>

<div>
    <div class="lefty">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
    Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount
    </div>
</div>
<br /><br />
<div>
@Html.ActionLink("Download in Excel Format", "PaExcelOutput", new {Versions = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter})
</div>
4

1 回答 1

1

您可以使用动态查询,它允许您使用动态列名。

于 2012-05-08T17:16:18.127 回答