Using ASP.NET MVC with generic views - asp.net

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..

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 %>

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.

Related

Html helpers (from asp.net mvc) in webpages

Is it possible to use MVC Helpers in webpages (cshtml) ?
I'm trying out some controls from infragistics, and would like to use the html helper method to create the grid (avoiding some javascript) but I can't seem to get any intellisense.
No, I'm unsure if it should work?
Update: I want to use the following line:
#Html.Infragistics().Grid(....
inside my MyWebPagesPage.cshtml
Thanks for any help
Larsi
You should be able to, just add
#using infrajistics.namespace
You could also add the namespace to ur web.config namespaces section so that you don't have to add the #using in each view
There are no helper method that would render Infragistics controls. Please take a look at this page, it has detailed instructions of using Infragistics controls in MVC pages.
There are some limitations though. Infragistics doesn't have separate controls for MVC, they are simply making their asp.net controls available, but there's a drawback.
As long as you focus on areas of the controls that do not initiate post backs or rely on ViewState, you soon find many behaviors and functions that work perfectly in an ASP.NET MVC application.
Sample usage would be:
<%# Register Assembly="Infragistics.Web.Mvc" Namespace="Infragistics.Web.Mvc" TagPrefix="cc1" %>
<%# Register Assembly="Infragistics35.Web.v9.1, Version=9.1.20091.1015, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
Namespace="Infragistics.Web.UI.GridControls" TagPrefix="ig" %>
<ig:WebDataGrid ID="wdg"
runat="server" Width="50%"
EnableViewState="false">
</ig:WebDataGrid>
No MVC helpers involved.

Import HTML page in .NET

The following code imports an HTML page (which simply holds one table) into my .ASP page. This works great, but I now am converting into .NET and am having obstacles.
<% Response.Write(getFilesContent("table.htm")) %>
This code does not work in .NET, and I read that this method is not recommended or widely used? Are there any thoughts, advice, or solutions about this?
I simply want to import this HTML page to read in a content box within my .NET page. In essence, the .NET page is hosting the HTML table.
Any help would be greatly appreciated.
Thank you in advance for your time and help.
To get you started, take a look at the System.Net.WebClient class http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.80%29.aspx
In particular, the "DownloadXXX" methods.
You could put the table in an asp.net user control
A asp.net custom user control can act like an include that encapsulates asp.net
markup.
here is a tutorial:
http://ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html
After you create the control add a reference
<%#Register tagprefix="uc" Tagname="html" src="custom_html.ascx" %>
then just a the control markup (in this case <uc:html runat="server"/>)
It would be cool the create a control the reads an html file by adding a src property
Try this:
<% Response.Write(New StreamReader(Server.MapPath("~/table.htm")).ReadToEnd()) %>

What functions are allowed inside <%# ... %> tags?

In ASP.NET, what functions are allowed inside the <%# %> tags? I frequently use Databinder.Eval(), and I know some basic things like CStr(), but where can I find a complete list with documentation? I would look myself, but honestly I don't even know what the name of the <%# %> tags are.
It's funny that nobody really knows what these are called - I think in the ASP.NET MVC team they call them Code Nuggets. Others call them Code Rendering Blocks.
Anyway, this is essential reading: http://quickstarts.asp.net/QuickstartV20/aspnet/doc/pages/syntax.aspx
Here is some specific info on the <%# Data Binding Syntax: http://msdn.microsoft.com/en-us/library/bda9bbfx%28v=VS.100%29.aspx
And this helped me understand the Eval voodoo: http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx
Anything that is in scope. E.g. public/protected methods on your page, public methods in some referenced namespace/class, etc. In addition to things that are related to the current NamingContainer you're within.

Register User Control Issue

I have a user control registered at the top of my page:
<%# Register Src="/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
and I reference it in my page like this:
<pmc:Navbar runat="server" id="navbar"></pmc:Navbar>
but it does not know what <pmc:Navbar is. I cannot figure out why.
I'm using VS 2008, in a Web Application Project.
Maybe you should specify the path with ~: ... Src="~/Controls/User/Navbar.ascx" ...
Remove either the initial slash from the path to the control, or better still, prefix it with "~" :
<%# Register Src="Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
or
<%# Register Src="~/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
The first solution is flakey as it relies on the page existing in the root folder and the control existing below it. The second is the preferred as it will work from any page in your project.
You should also consider registering your user controls in your web.config, as it keeps things much neater, and tends to avoid path issues a little better.

Strongly-typed ASCX in WebForms 3.5?

I'm looking to get rid of the code-behind for a control in my WebForms 3.5 application. Again bitten by the bug of how it's done in MVC, I'd like to get a step closer to this methodology by doing:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
This gives me the parser error you'd expect, so I remembered back to when this was an issue awaiting a fix in the MVC Preview, and changed it to:
<%# Control Language="C#" Inherits="Core.DataTemplate`1[[Models.NewsArticle]]" %>
But this doesn't work either! How is it that the MVC team were able to harness this ability? Was it something special about the MVC project type rather than the latest VS2008 Service Pack?
Short of giving up and requiring future templates to have code-behind files, what are my best options to get this as close to the generic user control method as possible?
Well, it appears like I've managed to do it. After looking at the PageParserFilter implemented by the MVC team for ViewUserControl<T>, I was able to construct something similar for my own DataTemplate<T> purposes. Sweet. I can now use the line:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
And, without any code behind file, it parses! I'll report back if I find that I've broken something else in the process!
With WebForms you lose pretty much everything that makes them useful without a code behind page, because then VS can't auto generate the designer file that holds the actual definitions for all your runat="server" controls.
What you can do is have a common base page class, and make that generic:
public class DataTemplate<T> : Page {
public T Model {get;set;}
}
public partial class MyCodeBehindClass :
DataTemplate<Models.NewsArticle> {
...
}
This would allow all the drag-drop component stuff that WebForms does to work unhindered, while also allowing you to access a strongly typed model on the page:
<%# Control Language="C#" Inherits="MyCodeBehindClass" %>
<% foreach( var item in Model ) { %>
<!-- do stuff -->
<% } %>

Resources