views:

711

answers:

1

How can I set attribute to MVC2 user control defined in single file with content:

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

I'm searching declarative solution. Something like this:

<%[DefaultProperty("Items")]%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Thanks.

+1  A: 

You would need to create a new class that inherits from ViewUserControl

public class SpecialAttribute : Attribute { }

[Special]
public class MyUserControl : ViewUserControl
{

}

And then in your partial view you would use the Inherits attribute like this:

<%@ Control Language="C#" Inherits="MvcApplication1.CustomViews.MyUserControl" %>
John Nelson
Yes, it's correct, but it requires to a second file.
Dmitry Borovsky
@Dmitry why does it have to be a single file? I don't think that a declarative approach is possible. Also, if you have code in your MVC application that processes the attribute it becomes unstable because you can't guarantee the integrity of your view as compared to a compiled assembly.
John Nelson
I don't need to add custom attribute. I need create user control that should be able to contain other controls with syntax:<my:control> <div/><div/><etc/></my:control>. For create this control I need add some attributes to control type. And this is one obstacle in use single file.
Dmitry Borovsky
In ASP.NET MVC partial views can contain other partial views the same as your regular views via <% Html.RenderPartial("ViewUserControl1", Model); %>. It really sounds as if you're trying to use MVC like WebForms. Can you provide more detail about what you're doing?For example, what do you think that <%[DefaultProperty("Items")]%> should allow you to do? If you were able to add this attribute, what kind of code would that let you write?
John Nelson
I think you are right. I'm trying to create WinForms style control, not MVC partial view. I thought that I need to create menu control that can be used like: <my:menu> <my:menuItem href="link">Link1</my:menuItem> ... <my:menuItem href="linkN">LinkN</my:menuItem></my:menu>. But it's not correct in MVC. How do you think what is correct style of menu definition using MVC.
Dmitry Borovsky