tags:

views:

121

answers:

3

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 perisistance layer I'm trying to make use of to create a strongly typed view

public class ModelAsset : BufferedLinqEntity2, AssetManagementModel.IAmAnAsset { ... }

I'm trying to create my first mvc view.

+1  A: 

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>
Darin Dimitrov
When I go to create view, my controller is not listed as a source for a strongly typed view?
Maslow
How do you create your views? I've never used the ASP.NET MVC wizard for adding strongly typed views. Maybe your interface model type is not listed in the combobox? If this is the case just create your view manually as I did in my example.
Darin Dimitrov
You mean that your INTERFACE is not listed? That doesn't matter, just replace the Inherits template directive manually in your view.
Palantir
@Maslow Why would your controller be the source type for the view?
Dan Atkinson
doesn't creating a view in the controller violate the whole point of mvc? to break things apart into distinct layers?
Maslow
@Dan - I'm trying to make sense of Darin's example and how it fits into the MVC framework
Maslow
@Maslow, you don't create a view in your controller. You create a model which is passed to the view for rendering. That's the point of MVC.
Darin Dimitrov
@Darin - re:how do I create my views. This is my first view, I'm right clicking the asset folder I created in the views folder and selecting the View item.
Maslow
@darin re: creating a view in your controller. I'm trying to create a strongly typed view, does your answer pertain to that?
Maslow
A: 

See this blog post for more information on implementing strongly typed views in ASP.Net MVC.

Brett Bim
I read that post and the one that brought him about to writing the one you linked, and I am not understanding them.
Maslow
A: 

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>
Maslow
I had to do this for the index, create and edit pages. Since if left as an interface it did not autogenerate any of the properties.
Maslow