我有一個名爲關鍵字的單個文本框。 用戶可以輸入多個字符串進行搜索。 這在mvc3中是如何實現的? 我使用nhibernate作爲ORM。 我可以爲此創建標準嗎?如何搜索mvc3中單個文本框中輸入的多個字符串
編輯場景
我局部視圖來搜索基於以下值工作:
- 關鍵詞(多個字符串),工業(含功能區級聯下拉列表)//運作良好FunctionalArea //運作良好 活動(多個地點),工作經驗//運作良好
在控制器我從表單集合檢索這些值。 什麼樣的數據類型應該用於關鍵字和位置(字符串或字符串[])?
public ActionResult SearchResult(FormCollection formCollection)
{
IList<Jobs> JobsSearchResultList = new List<Jobs>();
//string[] keywords = null;
string location = null;
int? industry = 0;
int? functionaArea = 0;
int? experience = 0;
string keywords = null;
if (formCollection["txtKeyword"] != "")
{
keywords = formCollection["txtKeyword"];
}
//if (formCollection["txtKeyword"] != "")
//{
// keywordAry = formCollection["txtKeyword"].Split(' ');
// foreach (string keyword in keywordAry)
// {
// string value = keyword;
// }
//}
......retrieving other values from formcollection
....
//Now passing these values to Service method where i have criteria for job search
JobsSearchResultList = oEasyJobsService.GetJobsOnSearchExists(keywords,industry,functionaArea,location,experience);
return View(JobsSearchResultList);
}
在服務我已經做了這樣的:我面臨
public IList<EASYJobs> GetJobsOnSearchExists(string keywords, int? industryId, int? functionalAreaId, string location, int? experience)
{
IList<JobLocation> locationlist = new List<JobLocation>();
IList<Jobs> JobsList = null;
var disjunction = Expression.Disjunction();
ICriteria query = session.CreateCriteria(typeof(Jobs), "EJobs");
if (keywords != null)
{
foreach (string keyword in keywords)
{
string pattern = String.Format("%{0}%", keyword);
disjunction
.Add(Restrictions.InsensitiveLike("Jobs.keywords", pattern,MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike("YJobs.PostTitle",pattern,MatchMode.Anywhere));
}
query.Add(disjunction)
.Add(Expression.Eq("EASYJobs.Industry.IndustryId", industryId))
.Add(Expression.Eq("Jobs.FunctionalArea.FunctionalAreaId", functionalAreaId))
.Add(Expression.Eq("Jobs.RequiredExperience", experience)));
}
else
{..
}
JobsList = criteria.List<Jobs>();
}
的問題是:
- 在控制器如果我使用的String [],然後拆分(」 ,')不會將字符串拆分爲指定的分隔符。它將字符串傳遞給Service。
2.In服務我試圖用%{0}%替換字符串,帶空格的字符串用給定的分隔符替換/ concat()。 但是這裏的問題是它總是返回整個工作列表意味着沒有給出所需的輸出。 請大家幫忙......
已經嘗試過....可能是我缺少確切的鏈接...我的整個場景我在我的question.Pleas編輯通過它又來了... – 2012-03-22 06:58:45
當您執行foreach(關鍵字中的字符串關鍵字)時,它將字符串視爲字符數組。嘗試用foreach替換它(關鍵字中的字符串關鍵字.Split(「,」)) – AlexCuse 2012-03-22 13:47:34