---
title: "Asp.net MVC User Control ViewData"  
description: "Asp.net MVC User Control ViewData"  
author: "marcel ethan"  
published: 2013-02-04  
updated: 2013-02-04  
canonical: https://www.mindstick.com/forum/575/asp-dot-net-mvc-user-control-viewdata  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# Asp.net MVC User Control ViewData

Hi Everyone!

When a [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) renders a view based on a model you can get the [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) from the [ViewData](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username) [collection](https://www.mindstick.com/articles/1718/collections-in-java) using the [indexer](https://www.mindstick.com/blog/103/indexer-in-c-sharp) (ie. ViewData["[Property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp)"]). However, I

have a shared [user control](https://www.mindstick.com/forum/294/problem-in-access-the-control-inside-the-user-control) that I tried to call using the following:

return View("[Message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net)", new { DisplayMessage = "This is a test" });\
and on my Message control I had this:

<%= ViewData["DisplayMessage"] %>\
I would think this would render the DisplayMessage correctly, however, null is being returned. After a heavy dose of tinkering around, I finally created a

"MessageData" class in order to strongly type my user control:

public class MessageControl : ViewUserControl<MessageData>\
and now this call works:

return View("Message", new MessageData() { DisplayMessage = "This is a test" });\
and can be displayed like this:

<%= ViewData.Model.DisplayMessage %>\
Why wouldn't the DisplayMessage property be added to the ViewData (ie. ViewData["DisplayMessage"]) collection without strong typing the user control? Is this by

design? Wouldn't it make sense that ViewData would contain a key for "DisplayMessage"?

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)\

## Replies

### Reply by AVADHESH PATEL

Hi Marcel Ethan!

For display message, you can Eval Method as below

ViewData.Eval("Put your message here")


---

Original Source: https://www.mindstick.com/forum/575/asp-dot-net-mvc-user-control-viewdata

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
