mortenstarck
mortenstarck

Reputation: 2803

Bing Maps Infobox, with html view

Is it possibel to load an _PartialView in ASP.Net MVC 4. I no that it's possibel to load it with the content

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>'); 

But i have not found any examples using an cshtml view.

Update:

 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);

Upvotes: 0

Views: 786

Answers (1)

acuntex
acuntex

Reputation: 147

  1. Step: Put your html in a partial view
  2. Step: Install and Integrate JavaScriptModel ( http://jsm.codeplex.com )
  3. Step: You need the following method in your 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. Step: Now you can add partial views to javascript variables anytime you want. E.g. you could write in your controller action:

    this.AddJavaScriptVariables("MyJavaScriptVariable", RenderPartialViewAsString("MyBingPartialView"));
    
  5. Step: Edit your js.file:

    defaultInfobox.setHtmlContent(MyJavaScriptVariable);
    

Upvotes: 1

Related Questions