我有一个搜索方法适用于IEnumerable模型。
如何使其与简单模型配合使用?(或者它是如何称呼的。
控制器:
[HttpPost] //FOR SEARCH (WORKING)
public ActionResult Search(string searchNume)
{
List<Contact> contactsList;
if (string.IsNullOrEmpty(searchNume))
{
contactsList = db.Contacts.ToList();
}
else
{
contactsList = db.Contacts.Where(x => x.Nume.Contains(searchNume)).ToList();
}
return View(contactsList);
}
视图:
@using Demo.Model.Contact
@using (Html.BeginForm())
{
<th>
@Html.TextBoxFor(model => model.Nume)
</th>
<th>
@Html.TextBoxFor(model => model.Prenume)
</th>
<th>
@Html.TextBoxFor(model => model.Adresa)
</th>
<th>
@Html.TextBoxFor(model => model.Mentiuni)
</th>
<th>
<input type="submit" name="submitSearch" value="Search" class="btn btn-info"
onclick=" location.href='@Url.Action("Search", "Home")' " />
</th>
UPDATE1:将索引操作结果更改为搜索
UPDATE2:发布了更多索引视图
更新3:更改后重新发布代码
////Search GET
//[ChildActionOnly]
public PartialViewResult Search() // for displaying the initial view with all contacts
{
List<Contact> contactsList = db.Contacts.ToList();
return PartialView("Contacts", contactsList);
}
////Search POST
[HttpPost]
public PartialViewResult Search(string txtsearchNume)
{
List<Contact> contactsList;
if (string.IsNullOrEmpty(txtsearchNume))
{
contactsList = db.Contacts.ToList();
}
else
{
contactsList = db.Contacts.Where(x => x.Nume.Contains(txtsearchNume)).ToList();
}
return PartialView("Contacts", contactsList);
}
视图:
@using Demo.Models
@model Contact
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
<link href="~/Content/jquery-ui.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui.min.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script>
//Search
var url = '@Url.Action("Search", "Home")';
var filter = $('#Nume');
var results = $('#results');
$('#search').click(function () {
results.load(url, { txtsearchNume: filter.val() });
});
});
</script>
}
@using (Html.BeginForm())
{
@Html.TextBoxFor(model => model.Nume, null, new { id = "txtSearchNume", @class = "form-control" })
<button type="button" id="search">Search by Nume</button>
}
<div id="results">
@Html.Action("Search")
</div>
表单提交按钮具有重定向到 GET 方法并且不传递任何参数的onclick="location.href='@Url.Action("Search", "Home")'"
。您的Search()
方法标记为[HttpPost]
,并且需要一个名为 searchNume
的参数,因此您的onclick()
事件实际上并没有执行任何操作。根据您的注释,您可以使用 jquery 处理此问题(此示例假设您只想按属性Nume
进行搜索,如 Search()
方法所示)
Html(将<input type="submit" ..>
替换为)
<button type="button" id="search">Search by Nume</button>
<div id="results">
@Html.Action("Search") // to initially display all contacts
</div> // place holder for the search results
并添加以下脚本
var url = '@Url.Action("Search", "Home")';
var filter = $('#Nume');
var results = $('#results');
$('#search).click(function() {
results.load(url, { searchNume: filter.val() });
});
并修改控制器方法以返回包含已过滤联系人的部分视图
public PartialViewResult Search() // for displaying the initial view with all contacts
{
List<Contact> contactsList = db.Contacts.ToList();
return PartialView("_Contacts", contactsList);
}
[HttpPost]
public PartialViewResult Search(string searchNume)
{
// could make this IEnumerable<Contact> and avoid the extra overhead of .ToList()?
List<Contact> contactsList;
if (string.IsNullOrEmpty(searchNume))
{
contactsList = db.Contacts.ToList();
}
else
{
contactsList = db.Contacts.Where(x => x.Nume.Contains(searchNume)).ToList();
}
return PartialView("_Contacts", contactsList); // partial view
}
您的部分视图(_Contacts.cshtml
)可能看起来像(来自您上一个问题)
@model IEnumerable<Demo.Models.Contact>
<table class="table table-bordered table-hover">
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.ContactId)</td>
<td>@Html.DisplayFor(modelItem => item.Nume)</td>
....
</tr>
}
</table>
旁注:如果初始视图显示所有联系人,则可以通过使用javascript/jquery在客户端过滤列表来提高性能并避免调用控制器。网络上有很多例子,包括这个
搜索Contact
实体上的多个属性,则需要单独的搜索模型。
// model
public class ContactSearchModel
{
public string Nume { get; set; }
public string Prenume { get; set; }
public string Adresa { get; set; }
public string Mentiuni { get; set; }
}
// view
@using Demo.Model.ContactSearchModel
@using (Html.BeginForm())
{
<th>
@Html.TextBoxFor(model => model.Nume)
</th>
<th>
@Html.TextBoxFor(model => model.Prenume)
</th>
<th>
@Html.TextBoxFor(model => model.Adresa)
</th>
<th>
@Html.TextBoxFor(model => model.Mentiuni)
</th>
<th>
<input type="submit" name="submitSearch" value="Search" class="btn btn-info"
onclick=" location.href='@Url.Action("Search", "Home")' " />
</th>
这就是你使用它的方式:
// controller
[HttpPost] //FOR SEARCH (WORKING)
public ActionResult Search(ContactSearchModel search)
{
List<Contact> contactsList;
if (search != null)
{
var query = db.Contacts.AsQueryable();
if(!string.IsNullOrEmpty(search.Nume))
query = query.Where(x => x.Nume.Contains(search.Nume));
if(!string.IsNullOrEmpty(search.Prenume))
query = query.Where(x => x.Prenume.Contains(search.Prenume));
if(!string.IsNullOrEmpty(search.Adresa))
query = query.Where(x => x.Adresa.Contains(search.Adresa));
if(!string.IsNullOrEmpty(search.Mentiuni))
query = query.Where(x => x.Mentiuni.Contains(search.Mentiuni));
contactsList = query.ToList();
}
else
{
contactsList = db.Contacts.ToList();
}
return View(contactsList);
}