I'm by no means lazy, but already 2 days overdue on our current sprint in my first ever MVC project, so I've been using the Add View Wizard 开发者_运维百科to generate strongly typed views, but now that I have started using ViewData classes as view models, the wizard generates fields for ViewDataBase, and not my derived ViewData.
I think that the derived ViewData is built by a factory at runtime, and assume that is why the designer/wizard can only give me the base class properties, but is there anything I can do about this?
ProfK,
Here is what I tried (VS 2010, MVC2):
public class ViewDataBase
{
public int ID { get; set; }
public string Name { get; set; }
}
public class CustomerViewData : ViewDataBase
{
public string Address { get; set; }
}
Right clicked on my Action and created a strongly typed Details view using CustomerViewData. The following gets generated:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyApplication.Models.CustomerViewData>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
</head>
<body>
<fieldset>
<legend>Fields</legend>
<div class="display-label">Address</div>
<div class="display-field"><%: Model.Address %></div>
<div class="display-label">ID</div>
<div class="display-field"><%: Model.ID %></div>
<div class="display-label">Name</div>
<div class="display-field"><%: Model.Name %></div>
</fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) %> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
</body>
</html>
精彩评论