MVC我的共享_Layout中有一个搜索栏它可以在其他视图中使用,但不能在_Layout中使用

问题描述

基本上是标题所说的。我有一个名为books的视图,其中搜索栏可完美工作并给出结果。 _Layout共享视图中没有发生这种情况。我已经尝试了几种脚本和其他东西,但都无济于事。有什么建议吗?

这是_Layout

<!DOCTYPE html>

@model IEnumerable<GoodReads.Models.Libro>
<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <Meta charset="utf-8" />
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Nombre de aplicación","Index","Home",new { area = "" },new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Inicio","Home")</li>
                    <li>@Html.ActionLink("Registrar Turno","AltaTurno","Turno")</li>
                    <li>@Html.ActionLink("Buscar Libros","Books","Libro")</li>
                    <li>
                        @using (Html.BeginForm(FormMethod.Get))
                        {
                        <div>
                            @Html.TextBox("parametro")
                            <input type="submit" id="btnSearch" value="Some text"/>                                
                        </div>                               
                        }
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <div id="Books">
        
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer></footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts",required: false)
</body>
</html>

这是“图书”视图

@model IEnumerable<GoodReads.Models.Libro>
@{
      /**/
      ViewBag.Title = "Books";
}

<head>
    <link href="@Url.Content("~/Content/Style.css")" rel="stylesheet" type="text/css" />
</head>

<body>
    @foreach (var item in Model)
    {
        <div style="margin-left: 350px; margin-top: 50px;">
            <h1 class="title-font">
                @item.Title &nbsp;
                <small class="year-font">(@item.Year)</small>
            </h1>
        </div>
        <div style="margin-left: 350px; margin-top: 10px;">
            <p style="font-size: 22px;">by @item.Author</p>
        </div>
        <div style="margin-left: 350px; margin-top: 10px;">
            <p style="font-size: 22px;">@item.ISBN</p>
        </div>
    }
</body>

书籍控制器(通过数据库连接而不是直接在控制器中完成

    // GET: Libro
    public ActionResult Index()
    {
        return View();
    }  

    public ActionResult Books(string parametro)
    {
        List<Libro> listalibros = ADLibros.BuscarLibro(parametro);
        return View(listalibros);
    }

解决方法

问题在于您的表格。您没有设定其动作。如果您不告诉它,它将使用默认值,即呈现页面的控制器。因此,当您的Books控制器呈现页面时,此方法可以工作,因为默认控制器将是books控制器。您需要明确指定get表单的操作。要查看此问题,请使用浏览器开发人员工具检查“书籍”页面和其他页面的表单。

要解决此问题(假设您的书籍控制器称为BooksController),请将_Layout页面中的表单代码更改为此

@using (Html.BeginForm("books","books",FormMethod.Get))
{
    <div>
        @Html.TextBox("parametro")
        <input type="submit" id="btnSearch" value="Some text" />
    </div>
}

我们正在使用带有3个参数的Html.BeginForm重载。第一个是actionName,第二个是controllerName,第三个是FormMethod