在asp.net mvc应用程序中的iframe中呈现部分视图

render partial view inside an iframe within asp.net mvc application

本文关键字:现部 视图 iframe net mvc 应用程序 asp      更新时间:2023-09-26

我有一个asp.net mvc应用程序,其中有一个名为_GeoApi.cshtml的部分视图。

我需要在iframe中渲染它。

<iframe src='@{Html.RenderPartial("_GeoApi");}' id='fram' ></iframe> 

我得到一个生成的html代码:

<iframe<script src="/Scripts/External js/jquery-1.8.2.js">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBCVSBoBAUjAfb-Pfx_qq0ZKUHCitbzsl4&amp;libraries=places&amp;callback=initAutocomplete" async="" defer=""></script>
//content removed for brivety
 src='' id='fram' /&gt; 
                                </iframe<script>

所以我需要了解

  1. 这个错误的原因是什么
  2. 如何修复我的代码

由于<iframe>标记中有<script>标记,因此导致错误。

解决方案是简单地提供一个URL作为URL的src,在该URL中您将呈现分部,而不是尝试将内容放在iframe标记中。

<iframe src='/api/geoApi' id='fram' ></iframe> 

然后创建一个具有GeoApi操作方法的ApiController,该方法呈现"_GeoApi"部分视图。