Can ASP.net pages be used in ASP.net MVC? - asp.net-mvc

"You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on." - MSDN
Does that mean we can use the server controls in ASP.net web forms in MVC?
Thanks,

Any controls that post back to the server don't work with MVC because MVC doesn't use the same mechanism or page cycle to post back to the same page. Rather MVC pages are routed to a new URL, so there's no concept of a 'persistant' page as there is with Web Forms.
There's a lot of discussion about how this sort of thing should be handled in an MVC framework because there's no doubt that Web Forms make many things very easy. Currently in MVC the closest thing to 'controls' are the Mvc HTML Helper classes that provide simple (but not very configurable) control rendering.
Using MVC vs. Web Forms is full of trade offs (in both directions) and there's no doubt you'll end up writing more markup code in MVC to make up for what you lose with Web Forms. The advantage is that you have much mroe control over rendering and aren't tied to the sometimes complex page pipeline cycle of postbacks.

For Syntax,
Razor:
<ul>
#for (int i = 0; i < 10; i++) {
<li>#i</li>
}
</ul>
Web Form in mvc(You can use this too)
<ul>
<% for (int i = 0; i < 10; i++) { %>
<li><% =i %></li>
<% } %>
</ul>
İf you want to check more information please check below link
hope helps you.
Using ASP.NET Server Controls in MVC?

Related

Helper for PagedList supporting unobtrusive Ajax in ASP.NET MVC

I'm currently using PagedList (https://github.com/TroyGoode/PagedList/) to manage my paging in an ASP.NET MVC application.
As of today I have started converting some parts of the application to use AJAX, which ASP.NET MVC makes quite easy.
The first problem I have run into however is that the PagedList.MVC helper #Html.PagedListPager is not in any way compatable with unobtrusive AJAX.
All I really need to do is add some attributes to the paging links (see below) and the rest would be taken care of automatically. PagedListPager does not provide any way to do this however.
data-ajax="true" data-ajax-mode="replace" data-ajax-update="#SearchResults"
Has anyone run into this and found an elegant solution?
I have added support for unobtrusive AJAX:
https://github.com/TroyGoode/PagedList/issues/26#issuecomment-6471793
I believe this may be the most elegant solution.
#Html.PagedListPager((IPagedList)Model.Articles, page => Url.Action("Index", new { s = Model.SearchString, page = page }))
<script>
var pages = $('#pages a[href^="/"]');
pages.attr('data-ajax', 'true')
.attr('data-ajax-mode', 'replace')
.attr('data-ajax-update', '#SearchResults')
.attr('data-ajax-method', 'post');
</script>
Quick jQuery hack to add the necessary attributes to all links in order for them to be picked up by the unobtrusive ajax module.
The [href^="/"] part ensures that only the clickble links will be modified. If you don't use this, the greyed out Previous link will be clickable.

Does Razor syntax provide a compelling advantage in UI markup?

I notice Scott Guthrie is starting to mention Razor a fair bit on his blog but I'm just not that sure that it's a good fit for my style.
Granted it's a fairly unfamiliar style for someone who's pretty used to a "standard" sort of ASP.Net markup (content place holders and inline code), but it just feels like a lot of additional pages to manage and less clear markup to me.
What are other peoples' feelings on it? Is it something that you believe should be seriously considered when scaffolding new MVC pages or is it just trying to solve a problem that doesn't exist?
[Disclaimer: I'm one of the Microsoft developers on MVC and Razor, so I might be a bit biased :)]
We designed Razor to be a concise templating language that uses only the minimal necessary amount of control characters. I would say that large parts of your views can be expressed with fewer characters than the same code using the "traditional" WebForms syntax.
For example the following code snippet in ASPX syntax:
<% if(someCondition) { %>
<ol>
<% foreach(var item in Model) { %>
<li><%: item.ToString() %></li>
<% } %>
</ol>
<% } %>
Can be expressed as follows in Razor:
#if(someCondition) {
<ol>
#foreach(var item in Model) {
<li>#item.ToString()</li>
}
</ol>
}
While the ASPX version has 21 transition characters (the <% and %>), the Razor version has only three (#)
I would say that the advantages of Razor are as follows:
Concise syntax, which is very similar to the way you write regular C# code (check out the following recent blog post by Phil Haack comparing Asxp with Razor syntax: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx)
Automatic HTML encoding of output (which helps protect you from html injection attacks)
Built in (though not 100%) validation of your markup which helps you avoid unbalanced tags
The page-related concepts also map easily from what you have in ASPX
As you can see inline code is still allowed
Sections (which can be optional) are equivalent to content placeholders
Layout pages instead of Master pages
The concepts of full and partial views are the same
#functions { ... } blocks instead of <script runat="server"> ... </script>
In addition Razor has a number of useful concepts that I would say are better than what is available in ASPX:
#helper functions for really easy creation of functions that emit markup
#model keyword for specifying your view's model type without having to write a <%# Page ... directive with the full class name
I would like to think that we have tackled a real problem, which is to allow you to more easily write concise and standards-compliant views while at the same time providing you with ways to refactor common code.
Of course, not everyone will prefer the syntax which is why we are also fully supporting the ASPX view engine. In addition you can check out Spark and NHaml, which are two 3rd-party view engines that enjoy significant community following. The following blog post has a good comparison of the different offerings: Link
Personally I really appreciate the reduction in how many escape characters are used. Using <% %> gets very tedious when compared to #{} and is not nearly as syntactically appealing.
Moreover, writing a whole definition for the codebehind and page is simplified to a single #model model.
As also noted by marcind, not having to always include runat=server is very nice also.
Overall, I really appreciate using the Razor engine and find it not only makes things easier on me to develop but also makes code easier to read.

Looking for repeater type functionality in ASP.Net MVC

I am used to using a Repeater control in conventional ASP.Net web projects. I see ASP.Net MVC doesn't have this sort of thing. What should I be using here?
EDIT:
In response to the question, what am I trying to do that I can't achieve in the foreach. I guess I am trying to get a alternating row style. Also, it just feels somewhat wrong to have stuff other than markup in the view. But maybe I will get over that as I work with it. Thanks for the answers.
The simplest thing to use is a foreach loop.
What are you trying to do?
EDIT:
<% bool odd = false;
foreach(var row in something) { %>
<tr class="<%= odd ? "OddRow" : "EvenRow" %>">
...
</tr>
<% odd = !odd; } %>
To add to SLaks response.
You could encapsulate your html into a Partial View.
Call <%= Html.Partial("ViewName", optional_ViewModel) %>.
This might feel closer to the repeater control. Where the PartialView would be sort of like your item template.
This approach also lends itself to code reuse very nicely.
If you miss a lot of the features of WebForms, maybe you just need a richer view engine? Might I suggest the Spark View Engine? Like WebForms, there's lots of functionality included so you don't have to keep rewriting the same stuff and/or write a bunch of your own helpers.
alternating row style can be achieved by css styles ... BTW - each one funcionality from web forms can be achieved in mvc ... in the end all is html, js and css
You could even create an HTML helper for this.

How do I get static expression ActionLinks etc in ASP.NET MVC 1?

I've read loads of examples in which
<li><%= Html.ActionLink("Home", "Index", "Home")%></li>
is rewritten as
<li><%= Html.ActionLink<HomeController>(c => c.Index(), "Index")%></li>
However, with a fresh ASP MVC project, this doesn't compile. I've read its something to do with needing to add the ASP MVC Futures assembly. Is this correct and if so, where do I get it?
Yes, you need the ASP.NET MVC Futures assembly, it's available here for 1.0:
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

Dynamic 2-level menu in web apps

I'm looking to create a top-level horizontal menu, with a second-level horizontal menu below it. Clicking on the top level menu causes the second level menu to change, based on the top level one. clicking on a second level menu causes the content of the page to change.
(BTW - I'm looking to do all this in ASP.NET MVC, so if you have any insights specific to this framework, please share them).
I'm not sure what's the best way to do the two-level menu, so there are zero page refreshes while the user plays with the top level menu. (CSS menu? javascript manipulating the page? a combination of both?)
How do I make the app refresh/redirect/load only the bottom pane (the main content of the page), rather than the entire page? ASP.NET MVC is pretty much full-page oriented by default. Should I frame its pages?
I'm a hard-core server developer working on his first web app, so be gentle... :)
#Shachar, you're asking 2 questions in one post, that's not the best way to use this site. :-)
I'm not sure what's the best way to do the two-level menu, so there are zero page refreshes while the user plays with the top level menu.
In my comment I have linked you to the original Suckerfish article, which has become a very common / perhaps the most common way of doing this. David Liddle showed you a more more recent implementation of Suckerfish using jQuery; if you're already using jQuery then this is IMHO the best solution today.
Since you're asking about the ASP.NET MVC angle, Syncfusion has a package of GUI widgets for ASP.NET MVC, and Telerik is building one using jQuery and community involvement. Not sure if any of them have a menu widget yet...
How do I make the app refresh/redirect/load only the bottom pane (the main content of the page), rather than the entire page? ASP.NET MVC is pretty much full-page oriented by default.
Hmn, I don't think ASP.NET MVC is "full-page oriented", there are just many different opinions on how to do AJAX. ASP.NET MVC gives you excellent building blocks for AJAX, not the full solution.
First off, are you sure you want to do this? I think what you're referring to is "AJAH", Asynchronous JavaScript And HTML. From this article about AJAH: "With true AJAX, a call is made to the server, the nicely formatted data is returned and the client application extracts the data from the xml, and replaces whatever elements need to be replaced on a page. With AJAH, a glob of html is returned and slapped into the page.".
AJAH isn't used that often today, for the following reasons:
You're not saving a significant amount of page weight (bytes) compared to a more conventional approach that uses caching to reduce subsequent page weights.
You're not separating data and presentation, so your code is less clean, and you're not building something that would be re-usable in an API or SOA approach later on.
IMHO the best use of AJAX right now, with regards to (usability improvement) versus (development and QA time) required, is to let high-ROI pages load datasets via AJAX. For example, a statistics section of a webapp might load as a normal page (with an initial dataset showing the graph the user most likely wants), and allow the user to change the graph via an AJAX call for new data.
IMHO you should also consider unobtrusive Javascript. It's kind of hard to explain, but it starts with a simple question: "What if the user agent doesn't support Javascript?". In my graph example above, the user would be out of luck, because he could not change the graph without Javascript. Here is a good presentation showing unobtrusive Javascript examples.
The other side of the coin is full-blown AJAX (like GMail), where more or less everything is built using Javascript UI widgets. This requires quite a lot of effort to build...
Assuming you want to go the AJAX route, here is one short introduction to AJAX using ASP.NET MVC and jQuery. After that, perhaps one of these books could be helpful?
Take a look at jQuery's superfish plugin
http://users.tpg.com.au/j_birch/plugins/superfish - go to Examples and nav-bar style
This works by building up a html list and uses jquery and css to style it.
<ul>
<li>Top level item 1
<ul>
<li>Sub item</li>
</ul>
</li>
<li>Top level item 2
<ul>
<li>Sub item</li>
</ul>
</li>
</ul>
Each item could/should relate to a controller/action method. You will then have to work out which controller/action you are viewing to make sure the correct menu item is selected for each page.
Here's a menu.ascx partial view I created.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% string menu = ViewContext.RouteData.Values["Controller"].ToString().ToLower(); %>
<% string submenu = ViewContext.RouteData.Values["Action"].ToString().ToLower(); %>
<div id="navigation">
<ul id="menu" class="nav-menu nav-navbar">
<% if (menu=="home") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Home", "Index", "Home") %>
<ul></ul>
</li>
<% if (menu=="configuration") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Configuration", "Index", "Configuration") %>
<ul>
<% if (menu=="configuration" && submenu=="page") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Pages", "Pages", "Configuration") %>
</li>
<% if (menu=="configuration" && submenu=="headline") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Headlines", "Headlines", "Configuration") %>
</li>
<% if (menu=="configuration" && submenu=="file") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Files", "Files", "Configuration") %>
</li>
<% if (menu=="configuration" && submenu=="rules") { %><li class="current"><% } else { %><li><% } %>
<%= Html.ActionLink("Application Rules", "Rules", "Configuration") %>
</li>
</ul>
</li>
...

Resources