0

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 -->

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.