I'm trying to implement my own RequiredAttribute, in which I call a custom resource handler:
public class LocalizedValidationAttributes
{
public class LocalizedRequiredAttribute : RequiredAttribute
{
private String _resourceString = String.Empty;
public new String ErrorMessage
{
get { return _resourceString; }
set { _resourceString = GetMessageFromResource(value); }
}
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
I call this the following way:
[LocalizedValidationAttributes.LocalizedRequiredAttribute(ErrorMessage = "test")]
public String Text { get; set; }
But the getter of ErrorMessage is never called.
Any hints? Thanks!
Try like this:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return ResourceManager.Current.GetResourceString(name);
}
}
or like this:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public LocalizedRequiredAttribute(string resourceTag)
{
ErrorMessage = GetMessageFromResource(resourceTag);
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
and then:
[LocalizedValidationAttributes.LocalizedRequiredAttribute("test")]
public String Text { get; set; }
Related
Am I missing any concept about inheritance? I am trying to call a method that is in a child class but it doesn't work.
This is what I have, so simple:
Vuelo.cs
public class Vuelo
{
private Aeronave _aeronave { get; set; }
public Vuelo(string numero, Aeronave aeronave)
{
ValidarNumero(numero); // validates numero
_aeronave = aeronave;
}
public string modelo_aeronave()
{
return _aeronave.model(); // This is where the error goes, .model()
}
public string RegistroAvion()
{
return _aeronave.Registration(); // This worked perfectly
}
}
Aeronave.cs
public class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
}
Airbus319.cs (the child class):
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public string model()
{
return _model;
}
}
I want to show up the model of the Airbus that is in model() like this:
Vuelo vuelo = new Vuelo("AB345", new AirbusA319("G-EUPT", "GG235"));
Console.WriteLine(vuelo.modelo_aeronave());
I can't find solutions in the internet, even in microsoft docs about inheritance.
You would need to modify your classes as shown below. Aeronave should contain model (virtual or abstract) to be overridden in Airbus.
public abstract class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
public abstract string model();
}
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public override string model()
{
return _model;
}
}
I have base class and some inherited classes, like this:
public abstract class Base
{
public long ID { get; set; }
public string Name { get; set; }
public virtual string Description { get { return "Base"; } }
}
public class A : Base
{
public override string Description { get { return "A"; } }
}
public class B : Base
{
private string extraInfo;
public override string Description { get { return "B"; } }
public string ExtraInfo
{
get { return extraInfo; }
set { extraInfo = value; }
}
}
I have collection of these objects and I set this collection to DataSource of DataGridView. Then, columns of DataGridView will be created by Base class's properties (ID, Name. Description). Is there any way to reflect also concrete implementation properties?
List<Base> items = new List<Base>();
DataGridView dgv = new DataGridView();
dgv.DataSource = new BindingList<Base>(items);
In this case, I want also column for ExtraInfo property of class B.
Inherit class B too:
public class Base
{
public long ID { get; set; }
public string Name { get; set; }
public virtual string Description { get { return "Base"; } }
}
public class A : Base
{
public override string Description { get { return "A"; } }
}
public class B : Base
{
private string extraInfo;
public override string Description { get { return "B"; } }
public string ExtraInfo
{
get { return extraInfo; }
set { extraInfo = value; }
}
}
I have a request like this:
filter[logic]:and
filter[filters][0][value]:a
filter[filters][0][operator]:startswith
filter[filters][0][field]:result
filter[filters][0][ignoreCase]:true
I need to receive it on the Controller but I don't know exactly how. I have tried this view model:
{
public class SearchFilterViewModel
{
public string logic { get; set; }
public List<SearchFilterFiltersViewModel> filters { get; set; }
}
public class SearchFilterFiltersViewModel
{
public string value { get; set; }
//public string operator { get; set; }
public string field { get; set; }
public bool ignoreCase { get; set; }
}
}
But the Controller receives it all null. operator property is commented because operator is a reserved keyword, I don't know how to make Asp.Net to use it. And I don't know if this is the cause of the problem.
Note that I can't change the request body pattern because it comes from this Kendo Widget.
This is my Controller(test version):
public ActionResult Text(SearchFilterViewModel filter)
{
return Json("", JsonRequestBehavior.AllowGet);
}
Here is working solution
Model:
public class SearchFilterViewModel
{
public string logic { get; set; }
public List<SearchFilterFiltersViewModel> filter { get; set; }
}
public class SearchFilterFiltersViewModel
{
public string value { get; set; }
public string oper { get; set; }
public string field { get; set; }
public bool ignoreCase { get; set; }
}
Then you can write custom IValueProvider where you can override usual parsing mechanism like this:
public class KendoValueProvider : NameValueCollectionValueProvider
{
public KendoValueProvider(NameValueCollection originalCollection)
: base(UpdateCollection(originalCollection), CultureInfo.InvariantCulture)
{
}
private static NameValueCollection UpdateCollection(NameValueCollection collection)
{
NameValueCollection result = new NameValueCollection();
foreach (string key in collection.Keys)
{
// ignore all other request
if (!key.StartsWith("filter"))
return null;
var newKey = key
.Replace("[filters]", string.Empty)
.Replace("filter[logic]", "logic")
.Replace("[value]", ".value")
.Replace("[operator]", ".oper")
.Replace("[field]", ".field")
.Replace("[ignoreCase]", ".ignoreCase");
var value = collection[key];
result.Add(newKey, value);
}
return result;
}
}
Then you need to write ValueProviderFactory that will register this ValueProvider like this:
public class KendoValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
return new KendoValueProvider(controllerContext.HttpContext.Request.QueryString);
}
}
And the last step is just register it in Global.asax file
ValueProviderFactories.Factories.Add(new KendoValueProviderFactory());
And sample Action
[HttpGet]
public ActionResult Index(SearchFilterViewModel model)
{
return Json(model, JsonRequestBehavior.AllowGet);
}
I have a business model which consists of a parent/child relationship (Identifier/IdentifierValue) and also some snapshot classes which look the same (IdentifierSnapshot/IdentifierValueSnapshot).
I am trying to create an extension method which will work on an enumeration of either Identifier or IdentifierSnapshot, but I just can't work out what to put in the extension method where I have inserted the XXX placeholder.
//Interfaces
public interface IIdentifier<TIdentifierValue>
where TIdentifierValue : IIdentifierValue
{
string Code { get; }
IEnumerable<TIdentifierValue> GetValues();
}
public interface IIdentifierValue
{
string Code { get; }
string Value { get; }
}
//Main classes
public class Identifier : IIdentifier<IdentifierValue>
{
public string Code { get; set; }
public IEnumerable<IdentifierValue> GetValues();
}
public class IdentifierValue : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
//Snapshots
public class IdentifierSnapshot : IIdentifier<IdentifierValueSnapshot>
{
public string Code { get; set; }
public IEnumerable<IdentifierValueSnapshot> GetValues();
}
public class IdentifierValueSnapshot : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
public static IdentifierExtensions
{
public static IEnumerable<XXX> ByCode<XXX>(this IEnumerable<XXX> instance, string code)
{
return instance.Where(x => string.Compare(code, x.Code, true) == 0);
}
}
I think this would do it:
public static IEnumerable<T> ByCode<T,Z>(this IEnumerable<T> instance, string code)
where T:IIdentifier<Z>
where Z:IIdentifierValue
Hello everyone I have create custom validation attribute and assign it to class level validation. Unfortunately, it is not called. I try every way that it think it could be solve the problem. However, it take me for hours and I can't find the attribute is not called by validation mechanism.
For illustrate you I put the following code.
Attribute
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class BooleanDependencyAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "กรุณากรอก{0}";
private readonly object _typeId = new object();
public string DependencyPropertyName { get; private set; }
public string DependentPropertyName { get; private set; }
public BooleanDependencyAttribute(string dependencyPropertyName, string dependentPropertyName)
: base(_defaultErrorMessage)
{
DependencyPropertyName = dependencyPropertyName;
DependentPropertyName = dependentPropertyName;
}
public override object TypeId
{
get
{
return _typeId;
}
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,name);
}
public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
bool dependencyValue = (bool) properties.Find(DependencyPropertyName, true /* ignoreCase */).GetValue(value);
object dependentValue = properties.Find(DependentPropertyName, true /* ignoreCase */).GetValue(value);
if (dependencyValue)
{
return true;
}
else
{
if (dependentValue == null)
{
return false;
}
else
{
return true;
}
}
}
}
ViewModel
[BooleanDependency("ReleaseNow","ReleaseDate",ErrorMessage="Please enter release date")]
public class ContentCreate
{
public string Title { get; set; }
public DateTime? ReleaseDate { get; set; }
public string Details { get; set; }
public string Abstract { get; set; }
public string Tags { get; set; }
public bool ReleaseNow { get; set; }
}
Please could you help me to solve this problem.
I found the solution. In fact validation in class level is called after all property-level validations are valid. Therefore I need to complete other required property then BooleanDependencyAttribute will called and valid value.
Thanks for view, edit the title and tag.