Posts Tagged ‘ASP.NET MVC Framework’

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

FYI – This still works for MVC4.

I am using the Razor view engine for this example; in Visual Studio 2010 with MVC 3 web application template. For more explanation, check out Scott Guthrie‘s blog here or here or one of the other many great posts he has written.

I like to use the SiteMap control for an easy horizontal navigation menu at the top of the content area and styled like a tab interface. Adding a few thin borders and the right background colors can create a great tab effect.

The new Razor view engine makes it very easy to create dynamic templates.

  • Create a SiteMap in the root directory of your web application and add some nodes
  • Create an App_Code folder in your solution if you do not have one already
  • Add a new blank partial view file called Helpers.cshtml in the App_Code folder
  • Enter the following into the file –

(more…)