我被卡住了。我正在尝试执行一个简单的搜索表单,但它不会返回任何结果。
这是我的控制器:
public ActionResult search(FormCollection frmCollection)
{
var search = frmCollection["searchString"];
List<JobViewModel> lst = new List<JobViewModel>();
var result = from a in db.jobs
join b in db.jobcategories on a.category_id equals b.id
join c in db.jobstatus on a.status_id equals c.id
select new
{
id = a.id,
title = a.title,
category_id = a.category_id,
category_name = b.name,
city = a.city,
state = a.state,
zip = a.zip,
status_id = a.status_id,
status_name = c.name,
description = a.description,
qualifications = a.qualifications,
required_education = a.required_education,
application_link = a.application_link
};
if (!String.IsNullOrEmpty(search))
{
result = result.Where(s => s.title.Contains(search));
}
foreach (var item in result)
{
var JobModel = new JobViewModel
{
id = item.id,
title = item.title,
category_id = item.category_id,
category_name = item.category_name,
city = item.city,
state = item.state,
zip = item.zip,
status_id = item.status_id,
status_name = item.status_name,
description = item.description,
qualifications = item.qualifications,
required_education = item.required_education,
application_link = item.application_link
};
lst.Add(JobModel);
}
return View("SearchResults", lst.AsQueryable());
}
这里是我的搜索表单:
@Html.ActionLink("View All Positions", "ListAll")
@using (Html.BeginForm("search", "JobSearchController", "search"))
{
<p>
Search: @Html.TextBox("searchString")
<input class="btn btn-primary btn-sm" type="submit" value="search" />
</p>
}
这是我的结果页面:
@model IEnumerable<Viaero.Next.Mvc.Models.JobViewModel>
RESULTS: @Model.Count()
POST: @Request["searchString"]
@if (Model.Count() > 0)
{
<table class="table table-hover">
<tbody>
@foreach (var o in Model)
{
<tr>
<td>
<span style="font-weight: bold"><a href="/about-viaero-wireless/careers/details/@o.id">@o.title</a></span><br /><br />
@(o.city + ", " + o.state)
</td>
<td><a class="btn btn-success btn-sm" target="_blank" href="@o.application_link">APPLY</a></td>
</tr>
}
</tbody>
</table>
}
else
{
<p>Sorry, there are no results. Please try again.</p>
}
问题是,无论我在搜索框中输入什么,我得到的结果都是零。然而,如果我将“where a.title.Contains(search)”替换为“where a.title.Contains(“某个字符串”)”它每次都能工作。
我已经验证了文本框的值被传递到视图,并且控制器可以看到它,但是它仍然不工作。有什么建议吗?
2条答案
按热度按时间iq3niunx1#
我猜搜索变量实际上是
null
,所以你没有得到任何匹配。尝试将传入参数更改为searchString
。这应该会给予你知道表单变量是否Map到参数。kxkpmulp2#
由于某种原因,添加SQL通配符不起作用,但是,下面的方法可以起作用。