views:

143

answers:

1

I have created a ViewUserControl in my ASP.NET MVC 2 project. This ViewUserControl serves as the general page-header for all views in the project.

How can I add a custom property on ViewUserControls, accessible from views using that control?..:

<%@ Register
    Src="../Shared/Header.ascx"
    TagName="Header"
    TagPrefix="uc" %>

<uc:Header
    runat="server"
    ID="ucHeader"
    MenuItemHighlighted="Menuitem.FrontPage" /> <!-- custom property, here -->
+3  A: 

Instead of creating user controls ala WebForms way I would suggest you the following:

Create a strongly typed user control Header.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<div><%: Model %></div>

And then simply include it in your pages:

<% Html.RenderPartial("~/Views/Shared/Header.ascx", "some value"); %>

In my example the user control is strongly typed to string but you could've used any custom type.

Darin Dimitrov