Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Wednesday, 26 October 2016

Sunday, 11 September 2016

Create SQL Server Database in Microsoft Azure

In this Article, we will learn how to create and set up a SQL Server Database in Microsoft Azure. In the previous article of this series, we saw



Note: You must have a Window Azure Account in order to perform below steps. You can create an account on Windows Azure using the following link http://www.windowsazure.com/en-us/pricing/free-trial/.

Let’s Begin:
First of all, you need to login in Microsoft Azure portal.

Wednesday, 10 August 2016

Thursday, 28 July 2016

HandleError Action Filter In ASP.NET MVC

In this article, you will learn how to handle errors using HandleError Action Filter in ASP.Net MVC. 
In previous ASP.NET MVC tutorials of this series, we saw,

In all the software Applications, Exception Handling plays a critical role. An exception can also be thrown during runtime, because of some mathematical calculations, some invalid data, or maybe because of network failure etc. ASP.NET MVC has HandleError Attribute (Action Filter), which provides one of the simplest ways to handle errors.

Monday, 11 July 2016

CRUD Operation In ASP.NET MVC Using AJAX And Bootstrap

This article shows, how to perform CRUD Operation in ASP.NET MVC, using AJAX and Bootstrap. In previous ASP.NET MVC tutorials of this series, we saw,


What is AJAX and Bootstrap?

AJAX (Asynchronous JavaScript and XML) in the Web Application is used to update parts of the existing page and to retrieve the data from the Server asynchronously. AJAX improves the performance of the Web Application and makes the Application more interactive.

Bootstrap is the one of the most popular HTML, CSS and JS framework for developing responsive, mobile first projects on the Web.



Monday, 13 June 2016

Inline and Custom Html Helpers in ASP.Net MVC

With the help of HTML Helper class, we can create HTML Controls programmatically. HTML Helpers are used in View to render HTML content. HTML Helpers (Mostly), is a method that returns a string.

HTML Helpers are categorized into three types:
1. Inline HTML Helpers
2. Built-in HTML Helpers
3. Custom HTML Helpers

In this Article, we will learn How to create inline and Custom HTML Helpers. We have already covered Built-in HTML Helper in the previous article of this series.

In previous ASP.NET MVC Tutorials of this series, we saw:


Inline HTML Helpers:
@helper syntax is used for creating reusable Inline helper method. They make the code more reusable as well as more readable. Let’s see how to use them.

Create an empty ASP.Net MVC application.
Right click on the controller and add a controller.

public class HomeController : Controller
{
       
    public ActionResult InlineHTMLHelper()
    {
        return View();
    }
}
Right click on Action Method and click on Add View.

@{
    Layout = null;
}

@helper OrderedList(string[] words)
{
    <ol>
        @foreach(string word in words)
        {
            <li>@word</li>
        }
    </ol>
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Inline HTML Helper</title>
</head>
<body>
    <div>
        @OrderedList(new string[]{"Delhi","Punjab","Assam","Bihar"})

    </div>
</body>
</html>
Preview:
In the above code, we have created an inline helper method i.e. OrderedList which takes a string array as an argument and displays each word in Ordered List. We can reuse inline HTML helpers multiple time on the same View.
In order to access the same inline HTML Helper across the different view, we have to move our @helper methods in .cshtml files to App_Code folder.

@helper OrderedList(string[] words)
{
    <ol>
        @foreach (string word in words)
        {
            <li>@word</li>
        }
    </ol>
}

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Inline HTML Helper App Code</title>
</head>
<body>
    <div>
        @InlineHelplerCode.OrderedList(new string[] { "Delhi", "Punjab", "Assam", "Bihar" })
    </div>
</body>
</html>

Preview:

Custom HTML Helpers:
We can create Custom HTML Helpers in two ways:
1. Using Static methods.
2. Using Extension methods.
Using Static methods:
This is one of the simplest method for creating Custom HTML Helpers. We have added a class (named as CustomHelper ) in CustomHelper folder.

public static class CustomHelper
{
        public static MvcHtmlString Image(string source,string altTxt,string width,string height){
            //TagBuilder creates a new tag with the tag name specified
            var ImageTag = new TagBuilder("img");
            //MergeAttribute Adds attribute to the tag
            ImageTag.MergeAttribute("src", source);
            ImageTag.MergeAttribute("alt", altTxt);
            ImageTag.MergeAttribute("width", width);
            ImageTag.MergeAttribute("height", height);
            //Return an HTML encoded string with SelfClosing TagRenderMode
            return MvcHtmlString.Create(ImageTag.ToString(TagRenderMode.SelfClosing));
        }

}
In the above code, we have created a static method that returns an HTML-encoded string using MvcHtmlString. Now Add namespace reference and call that Custom Helper from the view.
@{
    Layout = null;
}
@using InlineAndCustomHTMLHelper.CustomHelper
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Custom Static HTML Helper</title>
</head>
<body>
    <div>
        @CustomHelper.Image("/Images/UserPic.jpg","UserPic","100","100")
    </div>
</body>
</html>
Preview:
Using Extension Methods:
Extension method enables us to add new methods to an existing class. For creating an Extension method, we have to create a static class and first parameter of an extension method points towards the class that is extended by the method.
public static MvcHtmlString Image(this HtmlHelper htmlhelper, string source, string altTxt, string width, string height)
{
            var ImageTag = new TagBuilder("img");
            ImageTag.MergeAttribute("src", source);
            ImageTag.MergeAttribute("alt", altTxt);
            ImageTag.MergeAttribute("width", width);
            ImageTag.MergeAttribute("height", height);
            return MvcHtmlString.Create(ImageTag.ToString(TagRenderMode.SelfClosing));
}
In the above code, we have added Image Custom helper method and extended the HtmlHelper class. Now go to view, add the namespace reference and call Image helper method from HTML Helper class.
Preview:

Now we can use Image Html Helper in all views. The only thing we have to do is add the reference at the top so that we can call/invoke that custom helper method. If you want to use that custom helper method multiple times, you can add namespace of that custom helper class in the view’s web.config file.
<add namespace="InlineAndCustomHTMLHelper.CustomHelper"/>

I hope you like it. Thanks!

Monday, 23 May 2016

Working With Built-In HTML Helper Classes In ASP.NET MVC

In this Article, we will learn how to use Built-In HTML Helper Classes in ASP.NET MVC. In previous ASP.NET MVC Tutorials, we saw,

Using HTML Helper class, we can create HTML Controls programmatically. HTML Helpers are used in View to render HTML content. HTML Helpers (Mostly), is a method that returns a string. It is not mandatory to use HTML Helper classes for building ASP.NET MVC application. We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view. HTML Helpers are more lightweight as compared to ASP.NET Web Form controls as they do not use ViewState and does not have event model.

HTML Helpers are categorized into three types:

1. Inline HTML Helpers
2. Built-in HTML Helpers
3. Custom HTML Helpers

In this Article, we will cover Built-In HTML Helpers. We will see Inline and Custom HTML Helpers in the upcoming article of this series.
Built-in HTML Helpers are further divided into three categories:

1. Standard HTML Helpers
2. Strongly Typed HTML Helpers
3. Templated HTML Helpers

1. Standard HTML Helpers: 
Standard HTML Helpers use to render the most common type of HTML controls like TextBox, DropDown, Radio buttons, Checkbox etc. Extension methods of HTML Helper classes have several overloaded versions. We can use any one according to our requirement. Let’s see some of the Standard HTML Helpers:
Form: For creating Form element, we can use BeginForm() and EndForm() extension method.
BeginForm helper implements the IDisposable interface, which enables us to use using keyword.
Label: Label method of HTML helper can use for generating label element. Label extension method have 6 overloaded versions.
TextBox:
TextBox Helper method renders a textbox in View that has specified name. We can also add attributes like class, placeholder etc. with the help of overloaded method in which we have to pass objects of HTML Attributes.

We can also set the value In Textbox by passing a value in TextBox extension method.
TextArea: TextArea Method renders <textarea> element on view.
RadioButton:
RadioButton can be rendered in the view using the RadioButton Helper method. In the simplest form, RadioButton Helper method takes three parameters i.e. name of the control, value and Boolean value for selecting the value initially.
CheckBox: CheckBox helper method renders a checkbox and has the name and id that you specify.
In the above example, Did you noticed an Additional input element? In case if you unchecked the checkbox or checkbox value not selected then you will get the value from the hidden field.

DropDownlist: DropDownList helper renders a drop down list.
Password: Password Helper method renders input type as password.
Hidden: Hidden Helper method renders a Hidden field.

2. Strongly Typed Helper method: 
Just like Standard Helper, we have several strongly typed methods.
Html.TextBoxFor(), Html.TextAreaFor(), Html.DropDownListFor(), Html.CheckboxFor(), Html.RadioButtonFor(), Html.ListBoxFor(), Html.PasswordFor(), Html.HiddenFor(), Html.LabelFor() etc.

Strongly Typed Helper requires lambda expressions. For using Strongly Typed Helper method, Firstly We have to make Strongly Typed View.

Let’s create a simple example:
Create an empty MVC Application and Add a Student class in model.

public class Student
    {
        public int RollNo { get; set; }
        public string Name { get; set; }

        public string Gender { get; set; }
        public string City { get; set; }

        [DataType(DataType.MultilineText)]
        public string Address { get; set; }
           
    }
In Student class, I have added DataType attribute on Address property. I have added this attribute for showing a Templated HTML Helper example along with strongly Typed Helper method example. DataTypeAttribute class is present in System.ComponentModel.DataAnnotations namespace.

Now Add an empty controller and Name it as HomeController.


[HttpGet]
public ActionResult Index()
{
       return View();
}

 [HttpPost]
public ActionResult Index(Student stud)
{
       return View();
}

Right click on Index Action method for get request and click on Add View. Select Student Model class and Select empty template. Click on Add.
Go to view and Add the following code. I am not going to use any scaffolding template for this example.
[HttpGet]
@model StronglyTypedHTMLHelper.Models.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @using(@Html.BeginForm("Index","Home",FormMethod.Post)){
        <table>
            <tr><td>@Html.LabelFor(m=>Model.RollNo)</td><td>@Html.TextBoxFor(m=>Model.RollNo)</td></tr>
            <tr><td>@Html.LabelFor(m => Model.Name)</td><td>@Html.TextBoxFor(m => Model.Name)</td></tr>
            <tr><td>@Html.LabelFor(m => Model.Gender)</td><td>Male:@Html.RadioButtonFor(m => m.Gender, "Male", new { @checked="checked"})Female:@Html.RadioButtonFor(m => m.Gender, "Female", true)</td></tr>
            <tr><td>@Html.LabelFor(m => Model.City)</td><td>@Html.TextBoxFor(m => Model.City)</td></tr>
            <tr><td>@Html.LabelFor(m => Model.Address)</td><td>@Html.EditorFor(m => Model.Address)</td></tr>
            <tr><td></td><td><input type="submit" value="Submit"/></td></tr>
        </table>
        }
    </div>
</body>
</html>
In above code, we have bounded helpers with the model property using lambda expression. EditorFor is Tempate HTML Helper which will generate HTML element based upon the datatype of Address property of Student class.

Preview: 
Fill the form and click on submit. Request comes to Index Action method with [HTTPPost] attribute, from here we can get all posted values from the form and we can use that student object for any kind of Data Operations.

3. Template Helper Method:
These methods are very flexible and generate the HTML element based on the properties of the model class. We have already seen an EditorFor Helper method in the previous example, which generates TextArea element because we have declared MultiLine Datatype on Address property. Display, DisplayFor,Editor and EditorFor are the examples of Template Helper method.

Hope you like it. Thanks.
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook

Blog Archive