Copying FluentValidation Errors to the ModelState for a complex property? - razor-pages

When I follow the FluentValidation docs and copy the FluentValidationm error to the ModelState dictionary, only simple properties will cause asp-validation-for attributes to work. When I use a complex property it will not work unless I prepend the class name to the ModelState key.
.NET 7, FluentValidation 11.4.0, RazorPages.
HTML
<form method="post">
<div asp-validation-summary="All"></div>
<input type="text" asp-for="Sample.TestValue" />
<!-- Wont work unless prepend "Sample" to ModelState dictionary error key -->
<span asp-validation-for="Sample.TestValue"></span>
<button type="submit">Do it</button>
</form>
CodeBehind
namespace ValForTest.Pages;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class SampleValidator : AbstractValidator<Sample>
{
public SampleValidator()
{
RuleFor(x => x.TestValue)
.MaximumLength(1);
}
}
public class Sample
{
public string? TestValue { get; set; }
}
public class IndexModel : PageModel
{
[BindProperty]
public Sample Sample { get; set; }
public void OnPost()
{
var validator = new SampleValidator();
var result = validator.Validate(this.Sample);
foreach (var error in result.Errors)
{
this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
// This works!!! Code smell though. Better way to do this??
// this.ModelState.AddModelError($"{nameof(Sample)}.{error.PropertyName}", error.ErrorMessage);
}
}
public void OnGet() { }
}
Result:
asp-validation-summary works, asp-validation-for does not.
However, if I uncomment my // this works line where I add the "fully qualified" property name which includes the complex class name, then it will show the asp-validation-for span:
How can I tell FluentValidation to add the class name to the properties?

Related

Passing dynamically generated value to NUnit Custom Attribute

For our test scenarios - based on configuration of the application, we may want to either enable or disable a scenario. For this purpose, I created a custom IgnoreIfConfig Attribute like this :
public class IgnoreIfConfigAttribute : Attribute, ITestAction
{
public IgnoreIfConfigAttribute(string config)
{
_config = config;
}
public void BeforeTest(ITest test)
{
if (_config != "Enabled") NUnit.Framework.Assert.Ignore("Test is Ignored due to Access level");
}
public void AfterTest(ITest test)
{
}
public ActionTargets Targets { get; private set; }
public string _config { get; set; }
}
Which can be used as follows :
[Test, Order(2)]
[IgnoreIfConfig("Enabled")] //Config.Enabled.ToString()
public void TC002_DoTHisIfEnabledByConfig()
{
}
Now This attribute would only take a constant string as an input. If I were to replace this with something generated dynamically at the runtime, Such as a value from Json file - How can I convert it to a Constant. Constant Expression, TypeOf Expression or Array Creation Expression of Attribute parameter type ? Such as Config.Enabled ?
You can't do as you asked but you can look at the problem differently. Just give the attribute the name of some property in the JSON file to be examined, e.g. "Config".
As per Charlie's suggestion : I implemented it like this -
PropCol pc = new PropCol(); // Class where the framework reads Json Data.
public IgnoreIfConfigAttribute(string config)
{
pc.ReadJson();
if(config = "TestCase") _config = PropCol.TestCase;
// Here TestCase is a Json element which is either enabled or disabled.
}

Mvc5 pass model to Layout Page

I have an MVC 5 site with adminlte template. It's work fine. Now I need pass to some data to _layout page. I need pass to _layout page same info as number of alert, number of email, alert list ecc. I read same document about BaseController where do this operation, i.e. read this data and put in a model, or create an abstract model and put this info into. But is not possibile create this model one time (i.e. on user login controller) and share it in all request without re-create it every controller call? Pratically, as a global singleton variabile.
Thanks.
Looks like a good usecase to use a ChildAction which can be called from the layout view.
So start by creating a view model to represent the data
public class AlertVm
{
public int EmailCount { set; get; }
public int NotificationCount { set; get; }
}
Now create an action method which creates an object of this, set the values and pass to a partial view
[ChildActionOnly]
public ActionResult Alerts()
{
var vm = new AlertVm {EmailCount = 4, NotificationCount = 2};
return PartialView(vm);
}
Now your Alerts.cshtml view, which is strongly typed to our view model, you can render whatever you want.
<div>
<p>#Model.EmailCount emails</p>
<p>#Model.NotificationCount notifications</p>
</div>
And this action method can be invoked from the _Layout.cshtml view.
<div>#Html.Action("Alerts", "Home")</div>
With this approach, you do not need worry about the creating a view model for every single action. (Ex : Your about page which does not need a view model usually)
Yeah you can create a base view model & make all the model inherit it
public class MyModel
{
public MyBaseClass BaseClass { get; set; }
}
public abstract class MyBaseClass
{
public virtual string MyName
{
get
{
return "MyBaseClass";
}
}
}
public class MyDerievedClass : MyBaseClass
{
public int MyProperty { get; set; }
public override string MyName
{
get
{
return "MyDerievedClass";
}
}
}
only problem is ..the default CreateModel process doesnt register this deafult view model so global.asax is where you tweek it ..
Here is a good explanation

Type-Qualified binding in WinRt?

Is it possible to bind data using Type-Qualified (see Single Property, Attached or Otherwise Type-Qualified section) syntax in WinRT?
What I want to get is to have possibility to bind to an item of my ViewModel which is an interface:
public interface IViewModel {
INewsContainer ItemHost {get;}
}
public interface INewsContainer {
ObservableCollection<INews> News {get;}
}
class ViewModel: IViewModel, INewsContainer {
// ....
public INewsContainer ItemHost { get { return this; } }
// ...
ObservableCollection<INews> news;
ObservableCollection<INews> INewsContainer.News { get { return news; } }
}
Normally, in WPF binding like the following one works fine (assuming DataContext is an instance of ViewModel):
<ListView Grid.Column="1"
ItemsSource="{Binding Path=ItemHost.(vm:INewsContainer.News)}" />
But if I try doing so in WinRT it fails with log in Immediate Window:
A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred (...) Failed to assign to property 'Windows.UI.Xaml.Data.Binding.Path'. [Line: 35 Position: 17]
"Regular" binding, i.e. Path=ItemHost.News doesn't work either. It states that News property cannot be found in an instance of class ViewModel.
Workaround
This workaround works fine but I really hate having a converter over here :(
If you want to do that, you need to implement the interface both explicitly and implicitly.
Like this:
public interface IViewModel
{
string Name { get; set; }
}
public class ViewModel : IViewModel
{
public ViewModel()
{
(this as IViewModel).Name = "Jerry";
}
public string Name
{
get { return (this as IViewModel).Name; }
set { (this as IViewModel).Name = value; }
}
string IViewModel.Name { get; set; }
}
Then you can do this
<Grid Background="Black">
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<TextBlock Text="{Binding Name}" />
</Grid>
Read: http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx
Best of luck.

How to set up Entity Framework to map two classes to the same table

I've been bumbling along with EF5 but I cant seem to get two domain classes to map to a single database table.
The error I get is:
Message: "The type 'Basd.Erp.Wms.Purchasing.SupplierProfile' has already been configured as an entity type. It cannot be reconfigured as a complex type."
This is my DbContext:
public class PurchasingContext : DisconnectedEntityContext
{
public DbSet<SupplierCard> Suppliers { get; set; }
public DbSet<PurchaseCategory> PurchaseCategories { get; set; }
public PurchasingContext() : this("Basd.Erp.Wms") { }
public PurchasingContext(string connectionStringName) : base(connectionStringName) { }
public static PurchasingContext GetInstance(EfDataProvider provider) { return new PurchasingContext(provider.ConnectionStringName); }
}
}
These are my classes:
namespace Basd.Erp.Wms.Purchasing
{
public class SupplierCard : ContactCard, ISupplierCard
{
private ICollection<PurchaseCategory> _purchaseCategories;
public ICollection<PurchaseCategory> PurchaseCategories
{
get { return _purchaseCategories; }
set { SetNotifyField(ref _purchaseCategories, value, () => PurchaseCategories); }
}
public SupplierProfile Profile { get; protected set; }
private SupplierCard()
{
this.Profile = new SupplierProfile();
this.PurchaseCategories = new Collection<PurchaseCategory>();
}
public SupplierCard(long id, string alf, string name)
: this(id, alf, new SimpleNameHolder(name), new Collection<IPhysicalAddress>(), new DigitalAddresses()) { }
public SupplierCard(long id, string alf, INameHolder nameHolder,
ICollection<IPhysicalAddress> physicalAddresses, IDigitalAddresses digitalAddresses)
: this(id, alf, nameHolder, physicalAddresses, digitalAddresses, null) { }
public SupplierCard(long id, string alf, INameHolder nameHolder,
ICollection<IPhysicalAddress> physicalAddresses, IDigitalAddresses digitalAddresses, IValidatableObject validator)
: base(id, alf, nameHolder, physicalAddresses, digitalAddresses, validator)
{
this.Profile = new SupplierProfile();
this.PurchaseCategories = new Collection<PurchaseCategory>();
}
}
}
public class SupplierProfile : AbstractAspect
{
private TradingEntity _incType;
public TradingEntity BusinessType
{
get { return _incType; }
set
{
if (_incType != null) { this.DeregisterSubPropertyForChangeTracking(this.BusinessType); }
_incType = value; this.OnPropertyChanged("TradingType");
this.RegisterSubPropertyForChangeTracking(this.BusinessType);
}
}
private bool _emailOk;
private bool _smailOk;
public bool MarketingEmailOk
{
get { return _emailOk; }
set { _emailOk = value; this.OnPropertyChanged("MarketingEmailOk"); }
}
public bool MarketingSmailOk
{
get { return _smailOk; }
set { _smailOk = value; this.OnPropertyChanged("MarketingSmailOk"); }
}
public SupplierProfile()
: base()
{
this.BusinessType = new TradingEntity(ContactLegalType.Limited);
}
}
}
These are my configuration classes:
[Export(typeof(IEntityConfiguration))]
public class SupplierCardConfiguration
: EntityTypeConfiguration<SupplierCard>, IEntityConfiguration
{
public SupplierCardConfiguration()
{
this.ToTable("SupplierCard", "erp_wms");
HasKey(u => u.Id);
Property(u => u.Id).HasColumnName("SupplierId");
Ignore(u => u.UsePropertyNotifications);
Property(u => u.Profile.MarketingEmailOk).HasColumnName("MarketingEmailOk");
HasMany(i => i.PurchaseCategories)
.WithMany(c => c.Suppliers)
.Map(mc =>
{
mc.MapLeftKey("CategoryId");
mc.MapRightKey("SupplierId");
mc.ToTable("SupplierPurchaseCategory", "erp_wms");
});
}
public void AddConfiguration(ConfigurationRegistrar registrar)
{
registrar.Add(this);
}
}
[Export(typeof(IEntityConfiguration))]
public class SupplierProfileConfiguration
: EntityTypeConfiguration<SupplierProfile>, IEntityConfiguration
{
public SupplierProfileConfiguration()
{
this.ToTable("SupplierCard", "erp_wms");
Ignore(u => u.UsePropertyNotifications);
Property(u => u.MarketingEmailOk).HasColumnName("MarketingEmailOk");
}
public void AddConfiguration(ConfigurationRegistrar registrar)
{
registrar.Add(this);
}
}
UPDATE:
Ok so Ive tried ignoring SupplierProfile as per suggestion that changed nothing. I then tried removing the configuration class for Supplier Profile and left
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<SupplierProfile>();
base.OnModelCreating(modelBuilder);
}
and that generated an error:
{"The property 'Profile' is not a declared property on type
'SupplierCard'. Verify that the property has not been explicitly
excluded from the model by using the Ignore method or
NotMappedAttribute data annotation. Make sure that it is a valid
primitive property."}
[System.InvalidOperationException]: {"The property 'Profile' is not a declared property on type 'SupplierCard'. Verify that the
property has not been explicitly excluded from the model by using the
Ignore method or NotMappedAttribute data annotation. Make sure that it
is a valid primitive property."}
I then tried removing the
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<SupplierProfile>();
base.OnModelCreating(modelBuilder);
}
while leaving out the configuration class for SupplierProfile and that generates the error:
Message: "Invalid column name
'Profile_BusinessType_ContactLegalType'.\r\nInvalid column name
'Profile_BusinessType_TradingSince'.\r\nInvalid column name
'Profile_BusinessType_State'.\r\nInvalid column name
'Profile_BusinessType_UsePropertyNotifications'.\r\nInvalid column
name 'MarketingEmailOk'.\r\nInvalid column name
'Profile_MarketingSmailOk'.\r\nInvalid column name
'Profile_State'.\r\nInvalid column name
'Profile_UsePropertyNotifications'.\r\nInvalid column name
'OwnerId'.\r\nInvalid column name 'State'."
So like I said, just **bumbling** along ;)
After reading this I think it might have something to do with your relationship in your SupplierCard class.
public class SupplierCard : ContactCard, ISupplierCard
{
public SupplierProfile Profile { get; protected set; }
}
I'm guessing it registering as a complex type when SupplierCard is mapped.
A suggested way to fix it is to ignore it.
modelBuilder.Ignore<SupplierProfile>();
I've never run into this problem myself, so not sure if this'll help.
So after a lot of mucking around it turns out the underlying problem is a bug in Entity Framework 5. This bug has been fixed in EF6 beta. All other errors were in fact just masking this underlying error.
The following explaination is not terribly good as I dont fully understand it myself.
Short answer is: Use EF6 or otherwise modify EF5 source code.
Turns out that if you have a class in assembly B, that has a property of a type of enum defined in Assembly A, EF5 gets confused and thinks the enum is missing or somehow unavailable and sets about trying to generate the type itself.
So I had:
Assembly A containing enum type AA.
Assembly B referencing Assembly A so a contained class BB could have a property of type AA.
An EF5 data layer Assembly referencing both Assembly A & B.
An EF5 configuration layer Assembly referencing both Assembly A & B.
And it failed.
But if I "simply" move enum type AA into Assembly B then everything works.
This is of course is completely useless because then I set up all kinds of dependencies on Assembly B for any Assembly that has a member who needs an enum AA. But that is the test.
To top it off there also appears to be some particular set of circumstances in which everything I just said does not apply due to the order assemblies are loaded at runtime. The order of this loading cannot be forced i.e. it's non-determinant so it's pot luck.

Do Azure table services entities have an equivalent of NonSerializedAttribute?

If I'm trying to serialize a normal CLR object, and I do not want a particular member variable to be serialized, I can tag it with the
[NonSerialized]
attribute. If I am creating a table services entity, is there an equivalent attribute I can use to tell Azure table services to ignore this property?
For Version 2.1 there is a new Microsoft.WindowsAzure.Storage.Table.IgnoreProperty attribute. See the 2.1 release notes for more information: http://blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx.
There's no equivalent I know of.
This post says how you can achieve the desired effect - http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx
Alternatively, if you can get away with using "internal" rather than "public" on your property then it will not get persisted with the current SDK (but this might change in the future).
For version 2.0 of the Table Storage SDK there is a new way to achieve this.
You can now override the WriteEntity method on TableEntity and remove any entity properties that have an attribute on them. I derive from a class that does this for all my entities, like:
public class CustomSerializationTableEntity : TableEntity
{
public CustomSerializationTableEntity()
{
}
public CustomSerializationTableEntity(string partitionKey, string rowKey)
: base(partitionKey, rowKey)
{
}
public override IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
var entityProperties = base.WriteEntity(operationContext);
var objectProperties = this.GetType().GetProperties();
foreach (PropertyInfo property in objectProperties)
{
// see if the property has the attribute to not serialization, and if it does remove it from the entities to send to write
object[] notSerializedAttributes = property.GetCustomAttributes(typeof(NotSerializedAttribute), false);
if (notSerializedAttributes.Length > 0)
{
entityProperties.Remove(property.Name);
}
}
return entityProperties;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class NotSerializedAttribute : Attribute
{
}
Then you can make use of this class for your entities like
public class MyEntity : CustomSerializationTableEntity
{
public MyEntity()
{
}
public string MySerializedProperty { get; set; }
[NotSerialized]
public List<string> MyNotSerializedProperty { get; set; }
}

Resources