Currently I'm using a base class for some Customer model classes. As defined as follows
public abstract class BaseCustomerModel<T>
where T : IUserEntity
For further clarification, the IUserEntity contains:
public interface IUserEntity
{
string ErrorMessage { get; }
}
That property among some other things will be exposed in an ErrorTextControl.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<(...).Models.BaseCustomerModel<(...).IUserEntity>>" %>
<tr>
<td colspan="2"><%: Html.DisplayFor(model => model.ErrorMessage) %></td>
</tr>
I'll call the user control like so:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<(...).Models.MyUserModel>" %>
<% if (Model.HasErrors)
{
Html.RenderPartial("ErrorTextControl", Model);
} %>
Last piece of code (MyUserEntity implements IUserEntity):
public class MyUserModel : BaseCustomerModel<MyUserEntity>
Unfortunately this obviously doesn't work, because the generic type can't be implicitly casted to an IUserEntity. Therefore resulting in the following error message in the browser:
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type '(...).Models.MyUserModel', but this dictionary requires a model item of type '(...).Models.BaseCustomerModel`1[(...).IUserEntity]'.
So questions:
- Is there any way to define a user control with a generic base class from which the type is not yet known? If yes, how to accomplish such thing?
- If not, any workaround, common solution or ideas to be able to have a generic user control?
Many thanks.