views:

32

answers:

3

Hello Everybody

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.

Edit:

Question is edited cause misunderstanding possibility.

+1  A: 

You mean:

<%@ Import Namespace="System.Collections.Generic" %>

msdn

Sam Saffron
+1 I agree, depending on if you want it to be visible to ALL views or just a particular view correct? Now we just need someone to swoop in and combine these into a mega-answer and wal-mart us out of business ;)
TJB
+3  A: 

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

TJB
+1 both our answers are correct depending on the circumstances, if Im getting this question right
Sam Saffron
Can i pass List<int> instance from controller to view with this ?
Freshblood
Ok, that's something different (typing new answer...)
TJB
Should i edit my question or type new answer ?
Freshblood
@Freshblood Just edit your question to clarify thanx!
TJB
+3  A: 

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.

TJB
Yes, this is what i was asking.
Freshblood