Posts

Showing posts with the label ASP.Net MVC

ASP.Net MVC : ActionLink with bootstrap icon

Image
How many times you wanted to add an icon to your action link?!. using Html.ActionLink method is not possible to add icon as it renders the link with just HtmlAttribute you provide. ex : @Html.ActionLink("My Link","actionName","controllerName",new {id = Model.Id}, new {style="margin:10px", @class="btn btn-info"}); this will be rendered as shown in   if I want to add icon using bootstrap css classes, I can not use HtmlHelper methods and have to use plain html tags like this <a href="url-here" class="btn btn-info" style="margin:10px;">         <i class="glyphicon glyphicon-pencil"></i>         <span>My Link</span>     </a> this will be rendered  as so to simplify my .cshtml file and make it easy for developers to add icon to their action links, I have created an extension method IconActionLink that will replace the previous html sinppe...

AutoComplete using customized UIHint attribute in ASP.Net MVC

Image
In one project I worked on recently, I wanted to display list of suppliers which is very very long list, it was more than 6000 items. to display such number of items in dropdown list in a page of course will affect page load time. The best solution was using Autocomplete jQuery plugin , it is light, powerful, and covers my needs but unfortunately I need to display that suppliers list in many pages for different view models. One of possible solutions copy/paste autocomplete code snippet in each view.This solution didn't make me satisfied and I thought of course there is better way and more elegant to do it. One possible alternative is Editor Template, in most cases we need the Id value not the item full details. for simplicity I'm using list of programming languages as a data source. I override UIHint attribute to pass the URL of our datasource, the URL will be used as source for autocomplete jQuery plugin. public class AutoCompleteUIHintAttribute : UIHintAttribute {...

ASP.Net MVC : Conditional Validation using ValidationAttribute

In ASP.Net MVC we can validate each input field by decorating the corresponding property in the model class with some validation attributes like Required , MaxLength , MinLength ,...etc. Sometime we need to validate a property according to another property value for example if we have a registration form and we need the user enter his age if he is a male and age not required if the user sex is female. So if we decorated the Age property with Required attribute it will show error message even if the user is female, so we need to bypass this check with females. Assume our registration form will contains three fields, name, sex and age, and its model will be as follows public class RegisterationModel { [Required(ErrorMessage = "*")] public String Name { get; set; } [Required(ErrorMessage = "*")] [Display(Name = "Gender")] public Sex Sex { get; set; } [RequiredIf("Sex", Sex.Male, "enter your age")] pu...

Restrict non-ajax requests using ActionMethodSelectorAttribute

In ASP.Net MVC each coming request is routed to a certain action method inside some controller and with JQuery it became piece of cake to call this action method using ajax. In some scenarios we need to restrict the incoming requests to be ajax request only, fortunately ASP.Net MVC represent a new method to Request object called IsAjaxRequest() that return true if the current request come from ajax call and return false if current request is normal request(typing url in address bar of a browser, click a link to this page,..etc). We can use that method to deny any request come from ajax call or vise versa. public class HomeController { public ActionResult Index() { if(Request.IsAjaxRequest()) { //Do something } else { //Do something else } } } As we see if we need to make this check many times in our project, we will rewrite the same if statement for each action we need it be called via ajax only, but is there anot...

Load partial page in jquery and ASP.Net MVC

In this post I will show you how much ASP.Net MVC and JQuery are making up a dangerous duet and how you can do big tasks in simple way with little efforts. We can take a look at a live example of what we will do in this post. In stackoverflow website specially users page and tags page you will find a text box to search users/tags. the page is loaded with some users info sorted with default setting. if you start to write something in that text box, the users collection will be filtered by the name you entered in ajax manner and ordered alphabetically(ascending). The issue here is how using the same piece of code that produce the whole page when it be loaded in first time to filter the users when you type some letters in the text box and update just the part that display the users data. Here the magic of ASP.Net MVC and  JQuery  come to the scene. Lets start to build our page first then start to filter our users collection. I will not show you how to cr...

Generic CheckBoxList in ASP.Net MVC

In ASP.Net  web forms you can easily create a list of check boxes and bind it into a data collection using CheckBoxList control and specify which property will be the text of each check box and which one will be the value of the check box. You even don't need to write it, you can drag it from tools window, drop it into your page and set its properties and style with wizards that Visual studio provide. But in ASP.Net MVC there is no any server controls like that exists in web forms, all what we have is a helper methods to create HTML tags. In  ASP.Net MVC we have two helpers methods for creating a check box, Html.CheckBox and Html.CheckBoxFor that will be rendered as just one check box. If we want to create many check boxes or check boxes list we can call it inside a loop as follows @foreach (var item in MyCollection) { @Html.CheckBox("checkBoxName", new {value=item.checkBoxValue })@item.CheckBoxText<br /> } Here is a generic extension method for creat...

Safe ASP.Net MVC application against cross site attacks - 1

Do you trust user input?!!!!!!!. If your answer is YES, stop reading and do something else. If your answer is NO, I think reading this article may help you to make your site more secure. All user's inputs can be a back door for attacker to attack your site. User's inputs that we will categorize as un-trusted inputs are : Incoming URLs including Request.QueryString[] values Form post data ( Request.Form[] values including values from hidden fields and disabled fields) Cookies Data in HTTP Headers (such as Request.UserAgent and Request.UrlReferrer ). Your site could be attacked by altring the query string, form values, or cookies data. The solution is not to prevent request manipulation but to check that each request is a legal request for the logged-in visitor. Cross-Site Scripting and HTML Injection: If an attacker can get our site to return some javascript to our visitors, then the attacker's script can take control of our visitors' browsing session and...