I am working on an ASP.NET MVC Project and I have a number of node snippets that I need to insert into multiple pages. I would very much like to know whether or not it is possible to have a snippet of code that can be placed in an external file and called from within a ViewPage. In a way this would be similar to calling a class from a class.cs file.
Here is an example snippet of code:
<% Amazon.ECS.Model.Cart leftCart = ViewData["leftCart"] as Amazon.ECS.Model.Cart %>
<strong>Total: </strong>
<%= leftCart.SubTotal.FormattedPrice%><br />
<strong>Items: </strong>
<%= leftCart.CartItems.CartItem.Count%>
<center>
<a href="<%= leftCart.PurchaseURL %>">
<img style="float: right; margin-top: 20px;" src="/../../Content/Images/checkout.gif"
/></a></center>
Is it possible?
You can use view user controls. They are essentially ascx
files and behave roughly the same as their Web forms counterparts. The primary difference is that they inherit from System.Web.Mvc.ViewUserControl
instead of System.Web.UI.UserControl
.
You can either embed them using <% @Register %>
tags and <prefix:Name runat="server" />
or using Html.RenderPartial
method.