How can we pass class instances to view?
I just wonder that can we pass any type of class instances to view which is not exist in our Model repository.for example i just want to show property values of any class instance which exist in .net framework such as List, ListArray and others.
Ed开发者_JS百科it:
Question is edited cause misunderstanding possibility.
It seems you may be looking to create a strongly typed view This tutorial should show you the steps:
http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx
In short the declaration at the top of your view has to specify the type of model you expect from the controller like this
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Controllers.ProductViewModel>" %>
Where Inherits="...ProductViewModel" is whatever type you want the .Model
variable to be and thusly must be supplied by the controller.
You probably need to add some namespaces to the Web.Config.
<pages>
<namespaces>
<add namespace="System.Web.MVC"/>
<add namespace="System.Web.LINQ"/>
<!-- ... -->
<!-- Add more namespaces you need here -->
</namespaces>
</pages>
You indeed should be able to access any class in your app / includes within your views, but you have to qualify them with a namespace. Only a few namespaces are included by default.
Here is an article that explains how to add namespaces to the Web.Config so you can access classes in that namespace directly in your views:
http://davidhayden.com/blog/dave/archive/2009/10/13/ViewNamespacesInWebConfig.aspx
You mean:
<%@ Import Namespace="System.Collections.Generic" %>
msdn
精彩评论