Skip to content

IbanNet.DataAnnotations

skwas edited this page Dec 11, 2019 · 17 revisions

The IbanNet.DataAnnotations package provides support to validate IBAN user input with Microsoft DataAnnotations.

Usage

By property

public class InputModel
{
    [Iban]
    public string BackAccountNumber { get; set; }
}

// MVC
public class MyMvcController : Controller
{
    [HttpPost]
    public ActionResult Save(InputModel model)
    {
        if (ModelState.IsValid)
        {
            // ..
        }
    }
}

// Web API
public class MyWebApiController : ApiController
{
    [HttpPost]
    public IHttpActionResult Save(InputModel model)
    {
        if (ModelState.IsValid)
        {
            // ..
        }
    }
}

By parameter

public class MyController : Controller
{
    [HttpPost]
    public ActionResult Save([Iban] string bankAccountNumber)
    {
        if (ModelState.IsValid)
        {
            // ..
        }
    }
}

Dependency injection

The System.ComponentModel.DataAnnotations.ValidationContext provides an IServiceProvider for resolving dependencies. IbanNet uses this to resolve an IIbanValidator which is then used to validate the user input. If no instance of IIbanValidator is resolved from the DI container, the static Iban.IbanValidator property is used instead.

Additional info

For more detailed info please visit: