views:

82

answers:

2

I'm starting with MVC 2 in Visual Studio 2010. The thing is I'm working with a MasterPage where I'm rendering an action which returns a UserControl: Everything works well, I mean the user control has a lot of other labels and it's rendering other properties... So the problem has nothing to do with anything apart the img tag and the src attribute:

Site.master:

<div class="Test">
     <% Html.RenderAction("HeaderDetails", "User"); %>
</div>

HeaderDetails.ascx:

<img src="<%= Model.ImageUrl %>" />

For my surprise I cannot use the Model inside that attribute in a usercontrol, I have the same for a Page.aspx that is working.

The same happens with ViewData["ImageUrl"], I just don't have even intellicense in that attribute. It's like: I cannot do that.

Does anyone know why is that?, or how should I do it?

+1  A: 

I kind of got lost towards the end of your question, but in regards to the strongly typed Model in the user control, you'll need your page, masterpage and user control to all inherit with the same type arguments

page:

<%@ Page Language="C#" MasterPageFile="Your.Master" Inherits="System.Web.Mvc.ViewPage<YourModelType>" %>

master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourModelType>" %>

control:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<YourModelType>" %>

The type arg of the control and master can be base types of YourModelType, but obviously will need to have the ImageUrl property

jayrdub
Hi, thanks for response but that's not seems to be the problem. It happens only with the img tag and when I'm trying to assign a something for the Attribute. The surprise was that it works anywhere but usercontrols. I'm researching on creating a custom HttpHelper, so I'll bring it here if it's the solution.
Darkxes
+1  A: 

Well, I have solved it by creating a Custom HtmlHelper using Extension Methods going through this link.

The only matter is that I couldn't use the namespace MyMVCApp.Helpers, I had to use the System.Web.MVC one, so I'm not sure that would be the best way to do it.

If you have some input on that I will appreciate it.


So, I was missing the Import to MyMVCApp.Helpers inside the user control :D

Darkxes