Quote for the Week

"Learn to enjoy every moment of your life"

Tuesday, December 23, 2014

HTML Helpers in Asp.net MVC

HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.
You can also create your own HTML Helpers to render more complex content such as a menu strip or an HTML table for displaying database data.

Different types of HTML Helpers


There are three types of HTML helpers as given below:

1. Inline Html Helpers


These are create in the same view by using the Razor @helper tag. These helpers can be reused only on the same view.

helper ListingItems(string[] items)
 {
 <ol>
 @foreach (string item in items)
 {
 <li>@item</li>
 }
 </ol>
 }

 <h3>Programming Languages:</h3>

 @ListingItems(new string[] { "C", "C++", "C#" })

 <h3>Book List:</h3>

 @ListingItems(new string[] { "How to C", "how to C++", "how to C#" })

2. Built-In Html Helpers


Built-In Html Helpers are extension methods on the HtmlHelper class. The Built-In Html helpers can be divided into three categories-

a. Standard Html Helpers


These helpers are used to render the most common types of HTML elements like as HTML text boxes, checkboxes etc. A list of most common standard html helpers is given below:


TextBox @Html.TextBox("Textbox1", "val") 
Output: <input id="Textbox1" name="Textbox1" type="text" value="val" />

TextArea @Html.TextArea("Textarea1", "val", 5, 15, null) 
Output: <textarea cols="15" id="Textarea1" name="Textarea1" rows="5">val</textarea>

Password @Html.Password("Password1", "val") 
Output: <input id="Password1" name="Password1" type="password" value="val" />

Hidden Field @Html.Hidden("Hidden1", "val") 
Output: <input id="Hidden1" name="Hidden1" type="hidden" value="val" />

CheckBox @Html.CheckBox("Checkbox1", false) 
Output: <input id="Checkbox1" name="Checkbox1" type="checkbox" value="true" /> <input name="myCheckbox" type="hidden" value="false" />

RadioButton @Html.RadioButton("Radiobutton1", "val", true) 
Output: <input checked="checked" id="Radiobutton1" name="Radiobutton1" type="radio" value="val" />

Drop-down list @Html.DropDownList (“DropDownList1”, new SelectList(new [] {"Male", "Female"})) 
Output: <select id="DropDownList1" name="DropDownList1"> <option>M</option> <option>F</option> </select>

Multiple-select  @Html.ListBox(“ListBox1”, new MultiSelectList(new [] {"Cricket", "Chess"})) 
Output: <select id="ListBox1" multiple="multiple" name="ListBox1"> <option>Cricket</option> <option>Chess</option> </select>

b. Strongly Typed HTML Helpers


These helpers are used to render the most common types of HTML elements in strongly typed view like as HTML text boxes, checkboxes etc. The HTML elements are created based on model properties.

The strongly typed HTML helpers work on lambda expression. The model object is passed as a value to lambda expression, and you can select the field or property from model object to be used to set the id, name and value attributes of the HTML helper. A list of most common strongly-typed html helpers is given below:

TextBox @Html.TextBoxFor(m=>m.Name) 
Output: <input id="Name" name="Name" type="text" value="Name-val" />

TextArea @Html.TextArea(m=>m.Address , 5, 15, new{})) 
Output: <textarea cols="15" id="Address" name=" Address " rows="5">Addressvalue</textarea>

Password @Html.PasswordFor(m=>m.Password) 
Output: <input id="Password" name="Password" type="password"/>

Hidden Field @Html.HiddenFor(m=>m.UserId) 
Output: <input id=" UserId" name=" UserId" type="hidden" value="UserId-val" />

CheckBox @Html.CheckBoxFor(m=>m.IsApproved) 
Output: <input id="Checkbox1" name="Checkbox1" type="checkbox" value="true" /> <input name="myCheckbox" type="hidden" value="false" />

RadioButton @Html.RadioButtonFor(m=>m.IsApproved, "val") 
Output: <input checked="checked" id="Radiobutton1" name="Radiobutton1" type="radio" value="val" />

Drop-down list @Html.DropDownListFor(m => m.Gender, new SelectList(new [] {"Male", "Female"})) 
Output: <select id="Gender" name="Gender"> <option>Male</option> <option>Female</option> </select>

Multiple-select Html.ListBoxFor(m => m.Hobbies, new MultiSelectList(new [] {"Cricket", "Chess"})) 
Output: <select id="Hobbies" multiple="multiple" name="Hobbies"> <option>Cricket</option> <option>Chess</option> </select>

c. Templated HTML Helpers


These helpers figure out what HTML elements are required to render based on properties of your model class. This is a very flexible approach for displaying data to the user, although it requires some initial care and attention to set up. To setup proper HTML element with Templated HTML Helper, make use of DataType attribute of DataAnnitation class.
For example, when you use DataType as Password, A templated helper automatically render Password type HTML input element.


Display Renders a read-only view of the specified model property and selects an appropriate HTML element based on property’s data type and metadata. 

@Html.Display("Name")
@DisplayFor Strongly typed version of the previous helper 
@Html.DisplayFor(m => m. Name)

Editor Renders an editor for the specified model property and selects an appropriate HTML element based on property’s data type and metadata. 
Html.Editor("Name")
EditorFor Strongly typed version of the previous helper 
Html.EditorFor(m => m. Name)

3. Custom Html Helpers


You can also create your own custom helper methods by creating an extension method on the HtmlHelper class or by creating static methods with in a utility class.

public static class CustomHelpers
{
 //Submit Button Helper
 public static MvcHtmlString SubmitButton(this HtmlHelper helper, string 
 buttonText)
 {
 string str = "<input type=\"submit\" value=\"" + buttonText + "\" />";
 return new MvcHtmlString(str);
 }
 //Readonly Strongly-Typed TextBox Helper
 public static MvcHtmlString TextBoxFor<TModel, TValue>(this 
 HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>>
 expression, bool isReadonly)
 {
 MvcHtmlString html = default(MvcHtmlString);

 if (isReadonly)
 {
 html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
 expression, new { @class = "readOnly",
 @readonly = "read-only" });
 }
 else
 {
 html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
 expression);
 }
 return html;
 }
}

Tuesday, December 16, 2014

HashSet in C#.net

A HashSet holds a set of objects, but in a way that it allows you to easily and quickly determine whether an object is already in the set or not. It does so by internally managing an array and storing the object using an index which is calculated from the hashcode of the object.

For more information on hashing, check out the HashMap category.

Things to remember:
– A HashSet is not synchronized, so not thread-safe.
– Its elements are not ordered
– Add an element to the HashSet with the method add(Object o)
– Remove an element with the method remove(Object o)
– Remove all elements with the method clear()
– Get the number of elements with the method size()


Note:

This internally calls the UnionWith method to eliminate the duplications. ToArray transforms the HashSet into a new array.

Creation of Hashset<>

The following line of code describes how to create the hashset in C#:

HashSet<int> firstset = new HashSet<int>();
HashSet<int> secondset = new HashSet<int>();



Add elements in the Hashset<>

To add an element to the Hashset we use the add() method like:

for (int i = 1; i < 10; i++)
{
   firstset.Add(5 * i);
}
for (int j = 1; j < 10; j++)
{
   secondset.Add(10 * j);
}

Prints the elements of hashset using foreach loop

Write the following code to print the elements of the hashset:

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            HashSet<int> firstset = new HashSet<int>();
            HashSet<int> secondset = new HashSet<int>();
            for (int i = 1; i <= 10; i++)
            {
                firstset.Add(5 * i);
            }
            for (int j = 1; j <=10; j++)
            {
                secondset.Add(10 * j);
            }
            Console.WriteLine("The table of five(5) is:");
            foreach (int a in firstset)
            {
                Console.WriteLine(a);
            }
            Console.WriteLine("The table of Ten(10) is:");
            foreach (int b in secondset)
            {
                Console.WriteLine(b);
            }
        }
    }
}

Thursday, December 11, 2014

About New Asp.net vNext and MVC 6

Microsoft has announced the next generation of ASP.NET at TechEd North America on 12 May 2014, which is called ASP.NET vNext. ASP.NET vNext includes new cloud optimized versions of MVC6, Web API3, Web Pages4, SignalR3 and Entity Framework7.

Features of ASP.NET vNext

  • ASP.NET vNext includes new cloud-optimized versions of MVC, Web API, Web Pages, SignalR, and Entity Framework.
  • MVC, Web API and Web Pages have been merged into one framework, called MVC 6. This will follow common programming approach between all these three i.e. a single programming model for Web sites and services.
  • For example, there is unified controller, routing concepts, action selection, filters, model binding, and so on. In this way, You will have a single controller that returns both MVC views and formatted Web API responses, on the same HTTP verb.
  • MVC 6 has no dependency on System.Web since it was quite expensive. A typical HttpContext object graph can consume 30K of memory per request and working with small JSON-style requests this is very costly. With MVC 6 it is reduced to roughly 2K. The result is a leaner framework, with faster startup time and lower memory consumption.
  • ASP.NET vNext has new project extension project.json to list all the dependencies for the application and a startup class in place of Global.asax.
  • ASP.NET vNext apps are cloud ready by design. Services such as session state and caching will adjust their behavior depending on hosting environment either it is cloud or a traditional hosting environment. It uses dependency injection behind the scenes to provide your app with the correct implementation for these services for cloud or a traditional hosting environment. In this way, it will easy to move your app from on-premises to the cloud, since you need not to change your code.
  • .NET next version, .NET vNext is host agnostic. Hence you can host your ASP.NET vNEXT app in IIS, or self-host in a custom process.
  • .NET vNext support true side-by-side deployment. If your app is using cloud-optimized subset of .NET vNext, you can deploy all of your dependencies including the .NET vNext (cloud optimized) by uploading bin to hosting environment. In this way you can update your app without affecting other applications on the same server.
  • Dependency injection is built into the framework. Now, you can use your preferred IoC container to register dependencies.
  • .NET vNext use the Roslyn compiler to compile code dynamically. Hence you will be able to edit a code file and can see the changes by refreshing the browser; without stopping or rebuilding the project.
  • .NET vNext is open source and cross platform. It can run on Mono, on Mac and Linux.

Tuesday, December 2, 2014

A Four questions Survey about this blog..!!

Dear Subscribers and Followers..!!


Please specify your opinion about this blog on below link for future posts and services. It very important to know about your feedback for our service.

https://www.surveymonkey.com/s/F6DMQ3N

Thank you.

Monday, December 1, 2014

.NET is going to Open source and Cross-Platform

The next release of .NET will enable .NET cloud applications on multiple platforms, with future support of the core .NET server runtime and libraries for Linux and Mac. This implementation will be part of the .NET Foundation, home of the increasing number of .NET components being open sourced. Join and participate by visiting the .NET Foundation and the related GitHub repositories.


Download Visual Studio 2015 Preview and .NET 2015 Preview

These previews provide a first glimpse of Microsoft’s investment in an innovative set of technologies to help you efficiently build powerful, versatile applications across the platform spectrum – from Windows to web to iOS and Android. A few highlights include:

Cross-platform ASP.NET 5 websites

Multi-device app support, with Visual Studio Tools for Apache Cordova and the new Visual C++ tools for cross-platform library development.

Connected Services, enabling easier integration of services into apps including Office 365, SalesForce, and Azure Platform Services,
Smart Unit Testing (formerly PEX) technology from Microsoft Research.

For more info: