【问题标题】:Searching - from index view and result to another view搜索 - 从索引视图和结果到另一个视图
【发布时间】: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

【问题讨论】:

  • 在您的整个帖子中没有问号 (?)。你有什么问题?
  • 您的&lt;form&gt; 会向您发送Index() 方法。如果您想显示不同的视图,请返回到显示您的 searchresult.cshtml 视图的不同方法(尽管不清楚您为什么要这样做)
  • 因为索引是索引,我的意思是它只用于家庭内容,而不是任何东西。我只想让用户清楚地了解结果。所以当他/她搜索时,他/她应该只看到结果而不是屏幕上的任何其他内容,以避免混淆。
  • 创建一个 ajax 调用来搜索然后从控制器返回部分视图,在索引视图上创建 div 并将部分视图传递给特定视图,
  • 结果会显示在另一个视图中而不是索引中吗?我不确定部分视图是如何工作的。你能举个例子吗?

标签: c# asp.net-mvc search razor


【解决方案1】:

按照以下步骤操作:

-> 使用以下内容创建一个新视图 searchresult.cshtml

@model IEnumerable<MVC5_Search.Models.TblId>

<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>

-> 将您的索引发布操作修改为

[HttpPost]    
public ViewResult Index(string searching)
    {
     return View("searchresult",db.TblId.Where(x => x.IdNumber.Contains(searching) || searching == null));
    }

【讨论】:

  • 没有按预期工作。发生的事情是,索引只显示结果,但没有数据和没有结果!信息。除表格结果外,不显示索引的所有内容
  • 不太清楚你的意思!您是否想在索引视图中显示无结果消息并仅在有数据要显示时才显示 SearchResult 页面?
  • 没有。没有结果!仅当用户搜索在数据库中找不到的 id 时才会显示消息。结果将显示在 searchresult 视图中。我已经编辑了我的主要帖子。
  • 如果你的新Controller是SearchingController,那么BeginForm中的Verification是什么?应该是搜索!
  • 是的,它正在搜索,我的错误..typo
猜你喜欢
  • 1970-01-01
  • 2016-05-12
  • 2021-09-24
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多