0% found this document useful (0 votes)
110 views

Emailexcludehashattribute: Emailexcludehashattribute Validationattribute Validationresult Validationcontext

The document describes how to create a custom validator and custom HTML helper in ASP.NET MVC. For the custom validator, it shows creating a class that inherits from ValidationAttribute and overrides the IsValid method. For the custom HTML helper, it shows creating an extension method of HtmlHelper that generates an <img> tag.

Uploaded by

Santosh Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Emailexcludehashattribute: Emailexcludehashattribute Validationattribute Validationresult Validationcontext

The document describes how to create a custom validator and custom HTML helper in ASP.NET MVC. For the custom validator, it shows creating a class that inherits from ValidationAttribute and overrides the IsValid method. For the custom HTML helper, it shows creating an extension method of HtmlHelper that generates an <img> tag.

Uploaded by

Santosh Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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();

You might also like