AP.NET MVC

How to configure PageListGrid in MVC

Posted on

This article explains how to implement paging and sorting in MVC using PagedList with Ajax:

PagedList is a library that enables you to easily take an IEnumerable/IQueryable, chop it up into “pages”, and grab a specific “page” by an index. PagedList.Mvc allows you to take that “page” and display a pager control that has links like “Previous”, “Next”, etc.

PagedList grid

Configuration for loading Grid with Ajax :-

Install “PagedList.Mvc” via NuGet – that will automatically install “PagedList” as well.

  • Make sure you are adding the unobtrusive in the scripts section of the view as below code.
    View
  • Create a partial view for Grid which is (UnobtrusiveAjax_Partial) in the below example
    Partial View
  • Call PagedListPager method in the view by passing the list of parameters below:-
    data(“Names”),

    Action Name(“Index”),

    PageNumber ,

    PagedListRenderOptions – EnableUnobtrusiveAjaxReplacing is the option type in ajax based call.

    UpdateTargetId (“unobtrusive”)- the ID of the which need to be updated on paging.

    OnComplete (“PagedOnComplete”) – The javascript function PagedOnComplete() will be triggered OnComplete of Grid loading.

  • In Controller the action method must have ‘page’ as the first parameter to identify the page number

  • Return the partial view if it is AjaxRequest from client. So that the partial view will get return and the partial view will be reloaded.

Release Candidates for ASP.NET MVC 5.1

Posted on Updated on

Attribute Routing

The Attribute Routing will now support the constraints, versioning enabled and header based route section. Many aspects of attribute routes are now customizable via the IDirectRouteProvider interface and RouteProviderAttribute class.

There’s a Web API-based sample here:
(https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/RoutingConstraintsSample/ReadMe.txt)
the concepts are the same as MVC.

Bootstrap support in HTML Helpers

Now, you can pass HTML attributes in Html.EditorFor as an anonymous object. This means you can pass in Bootstrap styles in HtmlHelpers and style the various input elements such as textboxes, dropdownlist etc. with the correct Bootstrap styles.

Support for Enums

Now in ASP.NET MVC 5.1, the Enum type will be supported in Views. The new HTML Helper Html.EnumDropDownListFor() added will generate a dropdown when binding to Enum types. For this you need to declare an Enum as follows:

When you use the EnumDropDownListFor Helper, it will generate the following view when you are creating an Enum. You can see a full sample at:
https://aspnet.codeplex.com/SourceControl/latest#Samples/MVC/EnumSample/

References:-

http://www.asp.net/visual-studio/overview/2013/aspnet-and-web-tools-20132-preview-for-visual-studio-2013-release-notes
http://blogs.msdn.com/b/webdev/archive/2013/12/09/asp-net-and-web-tools-2013-2-preview-for-visual-studio-2013.aspx

ASP.NET MVC Pipeline Overview

Posted on Updated on

Asp.NET MVC Pipeline

Routing
is the first step in ASP.NET MVC pipeline. typically, it is a pattern matching system that matches the incoming request to the registered URL patterns in the Route Table.
The UrlRoutingModule(System.Web.Routing.UrlRoutingModule) is a class which matches an incoming HTTP request to a registered route pattern in the RouteTable(System.Web.Routing.RouteTable).
When ASP.NET MVC application starts at first time, it registers one or more patterns to the RouteTable to tell the routing system what to do with any requests that match these patterns. An application has only one RouteTable and this is setup in the Application_Start event of Global.asax of the application….. more

References:-
Dot net tricks.

Access modifiers in C#

Posted on Updated on

List of Access Modifiers in C#:

Public: When Members of a class are declared as public, then they can be accessed

1. Within the class in which they are declared.
2. Within the derived classes of that class available within the same assembly.
3. Outside the class within the same assembly.
4. Within the derived classes of that class available outside the assembly.
5. Outside the class outside the assembly.

Internal: When Members of a class are declared as internal, then they can be accessed

1. Within the class in which they are declared.
2. Within the derived classes of that class available within the same assembly.
3. Outside the class within the same assembly.

Protected: When Members of a class are declared as protected, then they can be accessed

1. Within the class in which they are declared.
2. Within the derived classes of that class available within the same assembly.
3. Within the derived classes of that class available outside the assembly.

Protected internal: When Members of a class are declared as protected internal, then they can be accessed

1. Within the class in which they are declared.
2. Within the derived classes of that class available within the same assembly.
3. Outside the class within the same assembly.
4. Within the derived classes of that class available outside the assembly.

Private: Private members of a class are completely restricted and are accessible only within the class in which they are declared.

References:-
http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx

Box Model in CSS

Posted on Updated on

The term “Box Model ” is used when we are talking about design and layout .

In CSS, all the HTML contents are considered as boxes

It consists of :-

1. Margin

2. Border

3. Padding

4. Content

Find the reference image below:-

Box model image

· Margin – Clears area around the border. The margin does not have a background color, it is completely transparent

· Border – Border goes around the padding and content. The border is inherited from the color property of the box

· Padding – Clears an area around the content. The padding is affected by the background color of the box

· Content – The content of the box, which is actual content

· To set the width and height of an element correctly in all browsers, you need to know how the box model works.

Note: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add the padding, borders and margins.

Example:

The total width of the element in the example below is 200px:

Calculation:
150px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 20px (left + right margin)
= 200px

Action filters features in MVC 5

Posted on Updated on

The Web MVC project templates integrate seamlessly with the new One ASP.NET experience. You can customize your MVC project and configure authentication using the One ASP.NET project creation wizard. An introductory tutorial to ASP.NET MVC 5 can be found at Getting Started with ASP.NET MVC 5 .

ASP.NET Identity

The MVC project templates have been updated to use ASP.NET Identity for authentication and identity management. A tutorial featuring Facebook and Google authentication and the new membership API can be found at Create an ASP.NET MVC 5 App with Facebook and Google OAuth2 and OpenID Sign-on .

Bootstrap

The MVC project template has been updated to use Bootstrap to provide a sleek and responsive look and feel that you can easily customize. For more information, see Bootstrap in the Visual Studio 2013 web project templates.

Authentication filters

Authentication filters are a new kind of filter in ASP.NET MVC that run prior to authorization filters in the ASP.NET MVC pipeline and allow you to specify authentication logic per-action, per-controller, or globally for all controllers. Authentication filters process credentials in the request and provide a corresponding principal. Authentication filters can also add authentication challenges in response to unauthorized requests.

Filter overrides

You can now override which filters apply to a given action method or controller by specifying an override filter. Override filters specify a set of filter types that should not be run for a given scope (action or controller). This allows you to configure filters that apply globally but then exclude certain global filters from applying to specific actions or controllers.

Attribute routing

ASP.NET MVC now supports attribute routing, thanks to a contribution by Tim McCall, the author ofhttp://attributerouting.net. With attribute routing you can specify your routes by annotating your actions and controllers.