我正在构建一个基于 WebMatrix 的站点,但我忽略了一些东西。我没想到的事情。
我的网站是这样设计的,每个页面都引用 _SiteLayout.cshtml 页面作为页眉和页脚。例如:
_SiteLayout.cshtml:
<html>
<head>
</head>
<body>
<div id="Container">
<div id="heading">
<input type="text" name="search" value="search" id="searchbox" />
</div>
@RenderBody() <!-- display Default.cshtml content -->
<div id="footer"><p>stuff here</p></div>
</div>
</body>
</html>
Default.cshtml:
@{
Layout = "~/_SiteLayout.cshtml";
}
<p>
I want to display search results in the Default.cshtml (homepage) page, but I don't know how to if the search form is inside the SiteLayout page. How to do this?
</p>
所以,我的问题是我想在 Default.cshtml(主页)页面中显示搜索结果,但如果搜索表单位于 SiteLayout 页面中,我不知道该怎么做。
我该怎么做?
最佳答案
您需要将该搜索输入中的信息发布到服务器。取回数据后,您可以使用它来定位您知道将在 subview 中的 HTML 元素。
This article来自 jQuery 文档帮助我。我的数据来自 JSON 响应,但该示例显示了如何使用 jQuery 获取结果响应并将其添加到特定元素(在本例中为 <body>
,但您可以轻松地创建一个 div “search-results”的 id 并使用 #search-results
定位它):
$.getJSON('ajax/test.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body'); // Here, you could append your items to #search-results
});
那么所有 View 都将包含一个带有搜索结果 ID 的部分,但如果它不存在,那么数据就不会显示在任何地方。该脚本应位于单独的文件中,并在您的主站点布局中引用。
关于c# - 如果搜索框位于 _SiteLayout 页面(在标题 DIV 中),如何将搜索结果返回给用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10551916/