Académique Documents
Professionnel Documents
Culture Documents
http://www.c-sharpcorner.com/uploadfile/jaishmathews/Asp-Net-mvc-3-...
In Focus
TECHNOLOGIES
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
LINKS
C# Corner Search
ARTICLE
ASP.NET MVC 3 Razor View And HTML Helpers
Posted by Jaish Mathews in Ar cles | ASP.NET MVC with C# on August 06, 2011 Tags: ASP.NET, ASP.NET MVC 3 Razor View, HTML, MVC 3, VS 2010, web applica on A er reading this ar cle, you will have a clear idea of the use of HTML helpers against razor view. I included 2 very simple examples of using an inline HTML helper inside the view itself and using HTML helper with an extension method and call the method inside view.
Tweet 0 Share Share 0
TRENDING UP
Insert, Select, Update and Delete Records in a Single Stored Procedure Using SQL Server Removing Item From the Playlist in Windows Store Apps Uploading and Downloading Excel Files From Database Using ASP.NET C# Stored Procedure in SQL Server 2012
10000
Reader Level:
Conver ng a Number To String in ASP.NET and C# Add Songs to Playlist in Windows Store Apps Dynamic Menu With Nested Submenus Using HTML List Tag and CSS in ASP.Net Media Playlist in Windows Store Apps Advanced Search in Windows Store Apps Searching Home Group Loca on in Windows Store Apps View All
I was just looking at the new version of ASP.Net MVC and its new view named razor. We need to learn a lot here. Otherwise you will screw up your application and you won't see the benefit of MVC layer inside your product. You can use either an aspx view or a razor view in MVC 3. Once using razor view, you won't see the page name inside the address like following screen shot.
After reading this article, you will have a clear idea of the use of HTML helpers against razor view. I included 2 very simple examples of: 1. Using inline HTML helper inside the view itself 2. Using HTML helper with an extension method and call the method in side view
I hope you have VS 2010 and ASP.Net MVC 3 already installed. Create a MCV project like the following. Please focus on areas marked with red.
HOT KEYWORDS
.NET Framework 3.5 ASP.net MVC 3 ASP.NET MVC Framework Asp.net mvc3 asp.net mvc3 Razor Aspx page AuthenticateRequest Cab Automation Software crud CRUD operation Cshtml Engine with Entity Framework 4.0 HttpHandlers JQ grid with Jquery in asp.net mvc3 Razor JSON Model View Control model
ASP.NET MVC
1 of 5
11/8/2012 8:23 PM
http://www.c-sharpcorner.com/uploadfile/jaishmathews/Asp-Net-mvc-3-...
SPONSORED BY
Oshore So ware and Database Development
MCN is your source for developing solu ons involving websites, mobile apps, cloud-compu ng, databases, BI, back-end services and processes and client-server applica ons.
view controller MVC 3 mvc application MVC Design pattern MVC3 routing Unity Framework View
Using HTML helpers as a declarative inline content In this simple example I am creating a radio button using HTML helper by declaring the logic inside the view file itself. Open the index.cshtml file. The Cshtml file is meant to handle HTML related functionality in a razor view implementation
Windows 8 and You What is your status with Microso Windows 8? I have installed it and ac vely using it. I have installed it but not using ac vely. I am planning to install it. May be in 3 to 6 months Not in near future
Vote
2 of 5
11/8/2012 8:23 PM
http://www.c-sharpcorner.com/uploadfile/jaishmathews/Asp-Net-mvc-3-...
Create a new class named Helpers (like the following) in a new folder:
The inner code is like below and you may already be aware that extension methods are not new and it needs a static class. I will not explain in depth about extension methods, but here we are adding one extension method to the built-in HtmlHelper class. namespace MvcApplication1.Utilities { public static class HtmlHelpers { public static DateTime GetTodayDate(this HtmlHelper helper) { return DateTime.Now; } } } The method merely returns the current date time. The "this" keyword is determining which class is being extended. Here it's HtmlHelper like GetTodayDate(this HtmlHelper helper). Now need to build the solution and use this new extension method inside the view file. Again open the same index.cshtml and the code will be like below: @using MvcApplication1.Utilities; Date from MVC html helper extension method @Html.GetTodayDate()</h2> The 1st statement is understandable as it's importing the required namespace. Once you imported the namespace, now you can use the extension method declared inside that namespace like @Html.GetTodayDate(). Just look at the method you defined can access with the built-in class, @Html i.e. HtmlHelper. This is the basic behavior of extension methods. Now run the application and see the output like below.
Last one more point. The configuration settings here are more flexible. Using this you can avoid the using statement of @using MvcApplication1.Utilities. This can be achieved by putting the same namespace inside config file under the views folder. See the following screen shot.
3 of 5
11/8/2012 8:23 PM
http://www.c-sharpcorner.com/uploadfile/jaishmathews/Asp-Net-mvc-3-...
Note that it's not application web.config, but specific to views. The section will be like this. <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="MvcApplication1.Utilities"/> </namespaces> </pages> </system.web.webPages.razor> Now this name space can access throughout view files. I uploaded the code too. You are expected to do more homework on top of it to dig in to these areas. Best wishes. Login to add your contents and source code to this ar cle
DynamicPDF Merger is a developers dream for interac ng with any exis ng PDF documents. Merge, append, split, form ll, a en stamp and so much more.
RELATED ARTICLES
Types of Operators in F#
POST COMMENT
TOP MEMBERS
MOST VIEWED
LEGENDS
NOW
PRIZES
AWARDS
REVIEWS
SURVEY
CERTIFICATIONS
DOWNLOADS
PHOTOS
TIPS
CONSULTING
TRAINING
STUDENTS
MEMBERS
MEDIA KIT
SUGGEST AN IDEA
PRIVACY POLICY | TERMS & CONDITIONS | SITEMAP | CONTACT US | ABOUT US | REPORT ABUSE
4 of 5
11/8/2012 8:23 PM
http://www.c-sharpcorner.com/uploadfile/jaishmathews/Asp-Net-mvc-3-...
5 of 5
11/8/2012 8:23 PM