One of the great MVC3 benefits is how easy it is to create helpers to inject HTML into your views.
After reading this post, I decided I would use Bart Czernicki’s example to create an MVC3 helper ‘cshtml razor engine style’.
You can place the following code in your App_Code folder (in a cshtml file).
I use a file called ‘Helpers’ and place multiple helpers in the same file. You could also create multiple files there to namespace your helpers if you have a bunch.
@helper SilverLightHostControl(
string silverlightXapFileLocation, // URI Location of the Silverlight XAP file
string minimumRuntimeVersion = "4.0.50826.0",
string objectContainerWidth = "100%",
string objectContainerHeight = "100%",
string onErrorJavaScriptFunctionName = "onSilverlightError",
string divObjectTagId = "silverlightControlHost",
string iFrameStyle = "visibility:hidden;height:0px;width:0px;border:0px"
){
<div id="@divObjectTagId">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="@objectContainerWidth" height="@objectContainerHeight">
<param name="source" value="@silverlightXapFileLocation"/>
<param name="minRuntimeVersion" value="@minimumRuntimeVersion" />
<param name="onError" value="@onErrorJavaScriptFunctionName" />
<param name="background" value="white" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
</div>
}
To use the helper in your view, call it like this -
@Helpers.SilverLightHostControl(“/ClientBin/SilverlightSurface.xap”)
For help integrating a Silverlight project in MVC, check this post out -
http://www.iwantmymvc.com/using-silverlight-with-mvc-and-json
Advertisement
Interesting to see the helper usage …