Maslow
Maslow

Reputation: 18746

Why can't an ASP.NET MVC strongly typed view use an interface?

Why can't I use this interface to create a strongly typed view?

public interface IAmAnAsset
{
    int assetID { get; }
    String assetTag { get; set; }
    int? AddedBy { get; set; }
    DateTime addedDate { get; set; }
    int? LocationId { get; set; }
    DateTime? purchasedDate { get; set; }
    int? purchasedby { get; set; }
    DateTime? disposalDate { get; set; }
    int assetModelId { get; set; }
    int? employeeId { get; set; }
    float? depreciated { get; set; }
    IAmAComputer Computer { get;  }
}

When I take that exact item and convert to an abstract class, it lets me create a strongly typed view.

I'm new but I would imagine there's something I'm missing, ASP.NET MVC can work with interfaces, right?

Here's the specific class in the persistence layer I'm trying to make use of to create a strongly typed view.

public class ModelAsset : BufferedLinqEntity2<LqGpsDataContext, asset>, AssetManagementModel.IAmAnAsset
{
...
}

I'm trying to create my first MVC view.

Upvotes: 0

Views: 927

Answers (2)

Maslow
Maslow

Reputation: 18746

So what I wound up doing is temporarily changing IAmAnAsset to an abstract class long enough so that the Create View drop down had it available,used that to create the View, then switched it back so the persistance/database layer would compile again.

Here's what it generated for me:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<AssetManagementModel.IAmAnAsset>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Assets
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>ListAll</h2>

<table>
    <tr>
        <th></th>
        <th>
            assetID
        </th>
        <th>
            assetTag
        </th>
        <th>
            AddedBy
        </th>
        <th>
            addedDate
        </th>
        <th>
            LocationId
        </th>
        <th>
            purchasedDate
        </th>
        <th>
            purchasedby
        </th>
        <th>
            disposalDate
        </th>
        <th>
            assetModelId
        </th>
        <th>
            employeeId
        </th>
        <th>
            depreciated
        </th>
    </tr>

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
            <%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
        </td>
        <td>
            <%= Html.Encode(item.assetID) %>
        </td>
        <td>
            <%= Html.Encode(item.assetTag) %>
        </td>
        <td>
            <%= Html.Encode(item.AddedBy) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.addedDate)) %>
        </td>
        <td>
            <%= Html.Encode(item.LocationId) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.purchasedDate)) %>
        </td>
        <td>
            <%= Html.Encode(item.purchasedby) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.disposalDate)) %>
        </td>
        <td>
            <%= Html.Encode(item.assetModelId) %>
        </td>
        <td>
            <%= Html.Encode(item.employeeId) %>
        </td>
        <td>
            <%= Html.Encode(item.depreciated) %>
        </td>
    </tr>

<% } %>

</table>

<p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

</asp:Content>

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

ASP.NET works perfectly fine with interfaces:

public interface IAmAnAsset
{
    int AssetID { get; set; }
}

public class AmAnAsset : IAmAnAsset
{
    public int AssetID { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        IAmAnAsset model = new AmAnAsset { AssetID = 10  };
        return View(model);
    }
}

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IAmAnAsset>" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <p><%= Model.AssetID %></p>
</asp:Content>

Upvotes: 2

Related Questions