0

是否可以在 ASP.Net MVC 4 中加载 _PartialView。我不认为可以将其与内容一起加载

var defaultInfobox = new Microsoft.Maps.Infobox(map.getCenter(), infoboxOptions );    
 map.entities.push(defaultInfobox);
 defaultInfobox.setHtmlContent('<div id="infoboxText" style="background-color:White; border-style:solid;border-width:medium; border-color:DarkOrange; min-height:100px;width:240px;"><b id="infoboxTitle" style="position:absolute; top:10px; left:10px; width:220px;">myTitle</b><a id="infoboxDescription" style="position:absolute; top:30px; left:10px; width:220px;">Description</a></div>'); 

但我没有找到任何使用 cshtml 视图的示例。

更新:

 var pushpinOptions = { icon: '../images/BingMap/discussion-icon-ny.png', width: 30, height: 40 };
                    var latLon = new Microsoft.Maps.Location(location.latitude, location.longitude);
                    var pin = new Microsoft.Maps.Pushpin(latLon, pushpinOptions);
                    pin.Title = name;//usually title of the infobox
                    pin.Description = set; //information you want to display in the infobox
                    pinLayer.push(pin);//add pushpin to pinLayer
                    Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);
4

1 回答 1

1
  1. 步骤:将您的 html 放在局部视图中
  2. 步骤:安装和集成 JavaScriptModel ( http://jsm.codeplex.com )
  3. 步骤:您需要在 Controller Base 中使用以下方法:

    public string RenderPartialViewAsString(string partialViewName, object model = null)
    {
        if (partialViewName == null)
            throw new ArgumentNullException("partialViewName");
    
        ViewDataDictionary viewData = new ViewDataDictionary(model);
    
        using (StringWriter writer = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, partialViewName);
    
            if (viewResult == null || viewResult.View == null)
                throw new InvalidOperationException(string.Format("Could not find partial view with name '{0}'", partialViewName));
    
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, viewData, TempData, writer);
            viewResult.View.Render(viewContext, writer);
    
            return Regex.Replace(writer.ToString(), @"[\t\n\r]", string.Empty, RegexOptions.Compiled);
        }
    }
    
  4. 步骤:现在您可以随时向 javascript 变量添加部分视图。例如,您可以在控制器操作中编写:

    this.AddJavaScriptVariables("MyJavaScriptVariable", RenderPartialViewAsString("MyBingPartialView"));
    
  5. 步骤:编辑您的 js.file:

    defaultInfobox.setHtmlContent(MyJavaScriptVariable);
    
于 2013-01-10T09:13:44.773 回答