Reputation: 2935
I have this idea where I would like to generate some forms in html and then display them in winforms in a web browser control. The hitch is that I am not sure how to go about doing so.
Is it possible to have one solution with an ASP.NET MVC project and a Windows forms (or WPF) project and call display the views from the asp.net project in a web browser control in the windows forms project?
I am trying to put together a quick test project and have done the easy stuff so far:
If somebody could explain to me how to go about displaying the MVC view in the winforms browser I'd be most grateful.
EDIT:
Basically what would like to figure out how to do is this:
In the form containing my web browser, I'd do something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DisplayReport();
}
public void DisplayReport()
{
this.webBrowser1.Navigate(MyMvcProject.Controller.MyCoolView);
}
}
And I'd get the html from MyCoolView displayed in the web browser control... I just don't have a clue how to do it, what using statements are required etc.
Upvotes: 1
Views: 1610
Reputation: 2623
I think it might be possible but you would have to create a bunch of object yourself. In a sense, it might be similar to unit testing where mocks are created for many objects.
In the past, I have used that function (from Inside a controller function) to create a string by rendering a partial view and it was used to generated emails from an ASP.NET MVC application.
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult =
ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext,
viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
Upvotes: 1
Reputation: 39248
This link shows how you can communicate between the web browser control and your wpf/winforms app. This works seamlessly and is a very neat way to facilitate two way communication between the browser control and the web page http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx
It requires a scripting object that you define as a regular class. Methods defined on this object can be called from the Website in JavaScript through the window.external.WhateverMethod
EX:
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
This calls the JavaScript method "test" - defined on the webpage (and passes a string variable.)
Upvotes: 1