Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.NET MVC with generic views

I am currently investigating MVC for a new project we are starting. So far I like it, but I'm wondering about something.

The actual views that we will be displaying will not be known at design time, we will be specifying in a config file somewhere how to build these views. Is this pattern supported by MVC or do we need to know at design time exactly what data we will be viewing?

If not, can someone give me some pointers on what I should be looking at as most of the info I have assumes that you have a model/view that is defined during your design.

Regards,

Alex..

like image 410
Fergal Moran Avatar asked Jul 03 '09 11:07

Fergal Moran


2 Answers

You can have your views weakly-typed... Your initial page directive on the view will look like:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

... and then you can refer to data from your Controllers like this:

<%= ViewData["MyData"] %>

Is there some common interface that you are intending to pass to your view? If so, you can benefit from a using the generic ViewPage<> :

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IamTheInterface>" %>

Then, you can use your interface to refer to your Model:

<%= Model.MyProperty %>
like image 127
Jeff Fritz Avatar answered Nov 04 '22 14:11

Jeff Fritz


There is cool post in LosTechies.com about building an "autoform" with fields autogenerated from the Model properties. Take a look, it might be what you are looking for.

like image 39
Ariel Popovsky Avatar answered Nov 04 '22 13:11

Ariel Popovsky