我有 2 个文本框需要用于搜索数据库。一个文本框用于代理城市,另一个文本框用于代理名称。似乎我错过了一些东西,因为当我尝试搜索代理城市时,它为我提供了所有代理机构,我在数据库中不仅仅是我正在搜索的城市。如有任何帮助,我们将不胜感激。
查看:
<!--Header-->
@{
Html.RenderPartial("~/Views/Shared/PageHeader.cshtml");
}
<br />
@model IEnumerable<Officer>
<!--Administer Agency,FLAgSearch, FLSearchAgency-->
<head runat="server">
<title>View Agency - Select Agency</title>
<!-- #include file="Content/Style.inc" -->
</head>
<body vLink="#0000ff" aLink="#0000ff" link="#0000ff">
<form id="Form1">
<table cellPadding="20" border="0" style="margin-left:-250px">
<tr>
<td vAlign="top" width="375" style="background-color:aliceblue">
<FONT face="Arial">
<STRONG><EM>Enter Part of Agency Name:</EM></STRONG>
</FONT>
<br>
<input type="text" id="AgName" name="AgName" width="250"/>
<br /> <br />
<FONT face="Arial">
<STRONG><EM>Or Enter City:</EM></STRONG>
</FONT>
<br>
<input type="text" id="AgencyCity" name="AgencyCity" maxlength="40" colmuns="20" width="250">
<br /> <br />
<input type="button" id="btnSearch" value="Search" onclick="func()" runat="server" style="width:100px">
<br /> <br />
<label id="lblStatus" runat="server"></label>
<input type="text" id="txtagencyid" runat="server" maxlength="5" columns="5" hidden="hidden">
</td>
<td vAlign="top">
<!-- DataGrid control to display matching Agencies -->
<div style="display:block" id="HideDiv" onclick="func()">
<p>To list agency officers, first search for the agency:<p>Enter all or part of an agency name in the text box</p><p>and click the Search button.</p>
<p>You can enter a maximum of 40 characters,</p><p>and the list will show you all of the agencies</p><p>whose name contain that word or set of characters.</p>
</div>
<table id="AgencyResults" class="table table-bordered table-striped"
style="width:100%;background-color:lightgray; display:none" CellPadding="5">
<thead>
<tr>
<th>
Agency Name
</th>
<th>
City
</th>
@foreach (var officer in @Model)
{
<tr>
<td>@officer.AgencyName</td>
<td>@officer.City</td>
<td>
@Html.ActionLink("Edit Officers", "Edit", new { id = officer.OfficerKey })
</td>
</tr>
}
</tr>
</thead>
</table>
<br />
<label id="lblMessage" runat="server"></label>
<br />
</td>
</tr>
<tr>
<td colspan="2">
<Label ID="TestData" CssClass="cite" runat="server"></Label>
</td>
</tr>
</table>
</form>
<script>
function func() {
//need to make if statements
//To show the table
document.getElementById('AgencyResults').style.display = 'block';
//To Hide the Div
document.getElementById("HideDiv").style.display = "none";
}
</script>
</body>
控制器:
public IActionResult Privacy(string AgencyCity)
{
if (_Db.Officers == null)
{
return Problem("Entity set 'Flipv2Context.Officer' is null");
}
var officers = from a in _Db.Officers
select a;
if (!String.IsNullOrEmpty(AgencyCity))
{
officers = officers.Where(o => o.City!.Contains(AgencyCity));
}
return View(officers.ToList());
}
型号:
using System.ComponentModel.DataAnnotations;
namespace CoreApp2.Models;
public partial class Officer
{
public int OfficerKey { get; set; }
public Guid? AgGuid { get; set; }
[Display(Name ="Agency")]
public string? AgencyName { get; set; }
public string? OfficerRank { get; set; }
[Display(Name = "Officer")]
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Phone { get; set; }
public string? Ext { get; set; }
public string? Cell { get; set; }
[Display(Name ="Address")]
public string? Address1 { get; set; }
public string? City { get; set; }
public string? State { get; set; }
public string? Country { get; set; }
[Display(Name ="Zip Code")]
public string? PostalCode { get; set; }
public string? Email { get; set; }
[Display(Name = "Date Modified")]
public DateTime? DateUpdated { get; set; }
public string? UserUpdated { get; set; }
public string? Address2 { get; set; }
public virtual ICollection<Language> Languages { get; set; } = new List<Language>();
}
当我尝试搜索代理城市时,它为我提供了所有代理机构, 我的数据库中不仅有我正在搜索的城市。
问题是您需要设置一个新变量,即搜索代理城市的新官员,而不是使用数据库中具有的相同变量官员。
更改以下内容:
var officers = from a in _Db.Officers
select a;
if (!String.IsNullOrEmpty(AgencyCity))
{
officers = officers.Where(o => o.City!.Contains(AgencyCity));
}
return View(officers.ToList());
至:
var officers = from a in _Db.Officers
select a;
if (!String.IsNullOrEmpty(AgencyCity))
{
var newofficers = officers.Where(o => o.City!.Contains(AgencyCity));
}
return View(newofficers.ToList());