Data Annotation (Custom Validator)
Steps:
1. Create a class for the custom validator followed by Attribute. Here, for example:
EmailExcludeHashAttribute
2. Inherit it from ValidationAttribute
3. Override IsValid method of ValidationAttribute class as in below snippet:
using System;
using System.ComponentModel.DataAnnotations;
namespace SampleMVCApp.Custom.DataAnnotations
{
public class EmailExcludeHashAttribute: ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if(value != null)
{
if(value is string)
{
if(value.ToString().Contains("#"))
{
return new ValidationResult("Hash is not recommended for email id. Please remove hash(\"#\") and try
again");
}
else
{
return ValidationResult.Success;
}
}
}
return ValidationResult.Success;
}
}
}
In model class file, use that custom validator as in below snippet:
public class LoginViewModel
{
[Required]
[Display(Name = "Email Id")]
[EmailAddress]
[EmailExcludeHash]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
Below is the scree-shot:
Custom Html Helper
Weve number of html helper available in mvc.
For ex.
1. @Html.LabelFor
2. @Html.TextBoxFor
3. @Html.ValidationMessageFor
4. @Html.CheckBoxFor
5. @Html.ActionLink
Etc.
Here, we dont have html helper for an image tag like - @Html.Image and we want that now. For that
we need to create custom html helper for image tag. Follow the steps below:
1. For this we need to create extension method of HtmlHelper class.
2. Create extension method as below code snippet:
3. Here, we can pass as many parameters as the control (img, here in this case) contains.
namespace SampleMVCApp.Custom.Helpers
public static class ImageHelper
{
public static MvcHtmlString Image(this HtmlHelper html, string src, string alt)
{
var tag = new TagBuilder("img");
tag.MergeAttribute("src", src);
tag.MergeAttribute("alt", alt);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.SelfClosing));
}
}
4. Build the solution and use the newly created html helper in cshtml file as below:
5. Before using the newly created html helper, we need to import the namespace used for the
creation of this cutom html helper.
@using SampleMVCApp.Custom.Helpers;
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
@Html.Image("https://titasiregar.files.wordpress.com/2009/04/firefox_logo_small.png", "Firefox logo")
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
Screen-shot below:
Singleton patter sample
public class TestClass
{
//Singleton Pattern
private static TestClass instance = null;
private TestClass()
{
}
private static object obj = new object();
public static TestClass GetInstance
{
get
{
lock (obj)
{
if (instance == null)
instance = new TestClass();
return instance;
}
}
}
....
public void update()
{
}
......
}
When you call this class methods:
TestClass.GetInstance.update();