MVC2 C# - Why I cannot assign a Model.ImageUrl to a img src attribute in a UserControl?
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.Image开发者_StackOverflowUrl %>" />
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?
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
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
精彩评论