Thursday, August 23, 2018

Customize the Error Message in ASP.NET MVC

Add the following keys into Application_Start() at Global.asax
ClientDataTypeModelValidatorProvider.ResourceClassKey = "YourResourceName";
DefaultModelBinder.ResourceClassKey = "YourResourceName";
Create YourResourceName.resx inside App_GlobalResources folder and add the following keys
FieldMustBeDate The field {0} must be a date.
FieldMustBeNumeric The field {0} must be a number.
PropertyValueInvalid The value '{0}' is not valid for {1}.
PropertyValueRequired A value is required.

Wednesday, July 25, 2018

Convert XML to C# Object

https://www.codeproject.com/Articles/1163664/Convert-XML-to-Csharp-Object

Get a property value based on the name

Add to any Class:
public class Foo
{
    public object this[string propertyName]
    {
        get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
        set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
    }

    public string Bar { get; set; }
}
Then, you can use as:
Foo f = new Foo();
// Set
f["Bar"] = "asdf";
// Get
string s = (string)f["Bar"];