views:

33

answers:

2

I'm new to Silverlight and I've used it to create a simple datagrid that I'd like to include as part of a view within an ASP.NET MVC View (while using the existing master page layout).

What's the best way to do this? I haven't been able to find any examples...

A: 

You would simply embed a Silverlight application containing your grid into your MVC View markup. ASP.NET MVC just outputs HTML of course so at a basic level all you are doing is adding an HTML object tag to your page markup that will load your Silverlight .xap file.

e.g.

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
          <param name="source" value="MySilverlightApplication.xap"/>
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.50401.0" />
          <param name="autoUpgrade" value="true" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=4.0.50401.0" style="text-decoration:none">
              <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>
        </object>

A potentially more tricky question is how will you load the data into your Silverlight grid control and that depends of course on where your data comes from. Typically data would be loaded into your Silverlight control via a call to a WCF web service.

Steve Willcock
Thanks for the quick response Steve! I've done this and updated the source etc but it doesn't seem to be executing the Silverlight app within the panel. Is there something I'm missing? Sorry for the noob questions :P
Myles
Firstly, you need to make sure the xap file referenced in the "source" element of the object tag is available and deployed in a directory in your site - in visual studio 2010 you can do this via the Properties dialog of the web site - there is a Silverlight Applications section in the properties where you can select Silverlight Applications that are part of the current solution to be automatically copied to the web directory structure when the project is built. Then you need to make sure the "source" value of your object tag is pointing to this xap file.
Steve Willcock
All this is appears to be set up properly and the standalone test page for the Silverlight app works fine, any other ideas? Thanks again!
Myles
Oops! In VS2010 I needed the source to be the full path i.e. value="/ClientBin/MySilverlightApplication.xap" Cheers Steve :)
Myles
A: 

Simply including the Silverlight application, via it's usual tag, is sufficient to include it in your view.

Are you asking how to "wire it up" to your data? That is more complicated, since the Silverlight object operates independently, and cannot access any data in your ViewModel.

If you need to pass it some initialisation parameters, the easiest way is to pass them via an "initParam" tag on the object:

<param name="initParams" value="your information here" />

Alternatively, you can access the query string from within the Silverlight application. Either way, if you need a lot of information you should look at exposing it via a webservice that your Silverlight application can then query.

Gareth Saul
Thanks Gareth, but I'm not looking to 'wire it up' to the db yet - I'll try walking before I run!
Myles