【发布时间】:2019-01-24 01:36:45
【问题描述】:
我正在 mvc5 中创建搜索功能 我的程序是这样工作的: 索引视图有搜索框和按钮 并且结果也显示在索引视图中
所以我的问题是我如何在另一个视图中显示结果 - 比如 searchresult.cshtml 而不是在索引视图中?
这是我的控制器:
public ActionResult Index(string searching)
{
return View(db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
}
我的索引视图(我只是删除了其他文本内容,只包含了搜索结果)
@model IEnumerable<MVC5_Search.Models.TblId>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
@Html.TextBox("searching")<input type="submit" value="Search" />
}
<table>
<thead>
<tr>
<td>Id Number</td>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
</thead>
<tbody>
@if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color: red">
No Result!
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>@item.IdNumber</td>
<td>@item.Firstname</td>
<td>@item.Middlename</td>
<td>@item.Lastname</td>
</tr>
}
}
</tbody>
</table>
我将它与实体框架一起使用
编辑:(试图解决)
这是我到目前为止所做的,
我创建了另一个控制器(SearchingController)以避免与主控制器冲突,
[HttpGet]
public ViewResult SearchResult(string searching)
{
return View("SearchResult", db.TblId.Where(x => x.TId.Contains(searching) || searching == null));
}
然后是视图 SearchResult.cshtml
@model IEnumerable<TblId.Models.TId>
@{
ViewBag.Title = "searchresult";
}
<table>
<thead>
<tr>
<td>IdNumber</td>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
</thead>
<tbody>
@if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color: red">
No Result!
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>@item.IdNumber</td>
<td>@item.Firstname</td>
<td>@item.Middlename</td>
<td>@item.Lastname</td>
</tr>
}
}
</tbody>
</table>
在我的索引视图中,
@using (Html.BeginForm("SearchResult", "Searching", FormMethod.Get))
{
@*@Html.TextBox("searching")*@
<input type="text" id="searching" name="searching" />
<button type="submit" name="searching" id="searching "class="btn btn-secondary">
Verify
<br>
</button>
}
仍然没有按预期工作
当我点击搜索按钮时,它只是将 url 更改为类似的内容
/Index?searching=T101&searching=
T101 - 是我正在搜索的 ID
【问题讨论】:
-
在您的整个帖子中没有问号 (?)。你有什么问题?
-
您的
<form>会向您发送Index()方法。如果您想显示不同的视图,请返回到显示您的searchresult.cshtml视图的不同方法(尽管不清楚您为什么要这样做) -
因为索引是索引,我的意思是它只用于家庭内容,而不是任何东西。我只想让用户清楚地了解结果。所以当他/她搜索时,他/她应该只看到结果而不是屏幕上的任何其他内容,以避免混淆。
-
创建一个 ajax 调用来搜索然后从控制器返回部分视图,在索引视图上创建 div 并将部分视图传递给特定视图,
-
结果会显示在另一个视图中而不是索引中吗?我不确定部分视图是如何工作的。你能举个例子吗?
标签: c# asp.net-mvc search razor