gpt4 book ai didi

html - 在 div/MVC3 中显示子网页

转载 作者:太空宇宙 更新时间:2023-11-04 15:30:29 24 4
gpt4 key购买 nike

我不确定这是否是常见情况,但我在 Google 上找不到任何关于如何执行此操作的信息。

在我的模型中,我有一个字符串属性,其中包含外部网站的整个 html。我想在我的页面上显示此 html 以显示整个网页,以及页面顶部的一些其他控件。

例如,假设我在页面顶部有一个带有搜索框的 div。我想在那个框中输入一个 url 并将其显示在下面的 div 中。我的隐藏代码将网页标记检索为字符串。

注意:似乎有使用 iFrame 的解决方案,但我不想传递 URL。我想将 html 作为字符串检索并以这种方式显示。

谢谢。

最佳答案

我认为您将不得不使用 iFrame - 但这还不错!你仍然可以显示你的字符串。

Controller 看起来像这样:

public class HomeController : Controller
{
//
// GET: /Home/
//Initial landing page
public ActionResult Index()
{
return View("");
}
//for full postbacks, sets the iframes src on the index view
public ActionResult Page(String url)
{
String myurl = "/Home/Search?url=" + url;
return View("Index", model: myurl);
}
//for the iframes src attribute
public ActionResult Search(String url)
{
//replace pageContent with your html string
String pageContent = "<html><head><title>this is the title</title></head><body>this is the body</body></html>";
return Content(pageContent);
}
}

索引将是您的着陆页或“搜索”页,如果不支持 javascript,页面将是您的表单发布到的位置,搜索将是您将 iFrame 指向的位置。

行动:

@model String
@{
ViewBag.Title = "URLer";
}
<script type="text/javascript">
$(document).ready(function () {
$('#searchForm').submit(function (e) {
e.preventDefault();
$('#page').attr('src', '/Home/Search?url=' + $('#url').val());
return false;
});
});
</script>
<div>
@using (Html.BeginForm("Page", "Home", FormMethod.Get, new { id = "searchForm" }))
{
<input id="url" name="url" type="text" />
<input id="Search" type="submit" value="Search" />
}
</div>
<iframe id="page" src="@Model" width="100%" height="500">
</iframe>

这显示了一个搜索框和提交按钮,使用 jQuery 表单提交将 iframe 的 src 属性设置为搜索操作,即“/Home/Search”,并将 url 文本框的值添加为查询字符串的一部分,然后这将触发 iFrame 导航到正在设置的 url(/Home/Search?url= http://google.com 作为示例),您返回自己的原始 html 页面/字符串而不是实际网站。

该表单是一个安全网,可能不需要,但是如果出于某种原因禁用了 javascript,该表单将发布到/Home/Page?url= http://google.com返回的 View 将设置 iframe,从而从我们的搜索操作中获取 url。

关于html - 在 div/MVC3 中显示子网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com