ASP.NET MVC Require positive number as input, save as negative in model - validation fails - asp.net-mvc-3

I want to model an 'Expense' object that has a 'Sum' (decimal) field.
In the view, I want to validate that the user enters a positive value.
OTOH I want to make sure I save the object with a negative value in the DB.
Right now, the model looks like this:
//------The model-------
public class Operation {
[Range(typeof(decimal), "0.0001", "79228162514264337593543950335")]
public virtual decimal Sum { get; set; }
[...]
}
public class Expense : Operation
{
public override decimal Sum
{
get
{
return base.Sum;
}
set
{
base.Sum = - Math.Abs(value);
}
}
}
//------In the controller-------
[HttpPost]
public ActionResult CreateExpense(Expense operation, int[] SelectedTags)
{
return CreatePost(operation, SelectedTags);
}
private ActionResult CreatePost(Operation operation, int[] SelectedTags)
{
if (ModelState.IsValid) // <-- this fails
[...]
}
The problem is, the MVC validation works with the object's properties (not the POST'ed form values), sees the negative value and fails to validate.
What should I do to fix this?
It looks to me like I'm not separating concerns (validate user input vs maintain database integrity).
Should I use a view model to hold the user input and then populate the actual model from the view model? Doesn't sound like KISS...

I found out that specifying a separate validation attribute on the property of the inherited class works a treat.
Can't think of something more straight-forward.
Here's how the model looks like now:
public class Operation {
public virtual decimal Sum { get; set; }
}
public class Income : Operation
{
[Range(typeof(decimal), "0.0001", "79228162514264337593543950335")]
public override decimal Sum
{
get { return base.Sum; }
set { base.Sum = Math.Abs(value); }
}
}
public class Expense : Operation
{
[Range(typeof(decimal), "-79228162514264337593543950335", "-0.0001")]
public override decimal Sum
{
get { return base.Sum; }
set { base.Sum = - Math.Abs(value); }
}
}

A validation attribute to check if the value is less than zero is another simple solution.
public class PositiveNumberAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object number, ValidationContext validationContext)
{
return int.Parse(number.ToString()) >= 0
? ValidationResult.Success : new ValidationResult("Positive value required.");
}
}
Then apply to property
[PositiveNumber]
public virtual decimal Sum { get; set; }

Related

How to access property of parent in validation attribute

In my code below I want to check with AttributeValidation if a field is given dependent on a property of its parent element. The comment in the class
RequiredIfParentState1
describes my question best.
public class ChildModel()
{
[RequiredIfParentState1]
public string ImRequired { get; set; }
}
public class ParentViewModel()
{
public int state { get; set; }
public ChildModel child = new ChildModel();
}
public class RequiredIfParentState1: ValidationAttribute, IClientModelValidator
{
RequiredIfParentState1()
{
}
void AddValidation(ClientModelValidationContext context)
{
}
protected override ValidationResult IsValid(object i_value, ValidationContext i_context)
{
var element = i_context.ObjectInstance;
if(i_value == null && //what do i have to put here to check if the state is 1?)
{
return new ValidationResult($"Field is Required in state 1.");
}
return ValidationResult.Success;
}
}
I feel this is the wrong approach.
An object being in a valid state is one thing (required fields and type checking), but handling business logic is a separate concern.
You could write a validation service, that examines the model in detail, checking business logic concerns, and build up a list of errors.
Where errors are found you can return these in your response.

ModelState.Isvalid invalidating field even though default value existed

public abstract class Base : IBase
{
[Required]
public int key {get;set;}
}
public class Entity: Base
{
public string Name {get;set;}
}
public class child : Entity
{
[Required]
public string Park {get;set;}
}
ActionFilter
public class ValidateViewModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false) {
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
Now, when value is posted to API then, not setting "Key" field as it is the request for SAVE. Problem is, above attribute says, MODEL IS INVALID for field "key" . Its already there as 0 value for Id field (as default int).
I expect, it should validate true as 0 is default value.
NOTE: I could not remove or make any change in BASEENTITY and PARENT entity above.
I have only control in CHILD entity and this attribute class.
To ignore a property that is marked as [Required] you can use ModelState.Remove("propertyName");
Also, your property has a value of 0 because an int cannot have a value of NULL so the 0 is automatically attributed. But if you did not pass this value in the form data, the model validation will "consider" that it is NULL and thus will make the model invalid. If you do not want to use the call to Remove as shown above, you will have to explicitly give a value to the Key property :-)
source: The first comment on this page - credit for this explanation #Stephen Muecke
Use something like
public class ValidateViewModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
ModelState.Remove("key");
if (actionContext.ModelState.IsValid == false) {
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
Note: By default, MVC6 model validation will simplicity tag all non-nullable value types as required (god knows why).
call
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
disable this behaviour.
I have the same problem and solved it by this way :
[AcceptVerbs("Post")]
public ActionResult EditingInline_Create([DataSourceRequest] DataSourceRequest request, Model.Server server)
{
if (server != null && ModelState.IsValid) //Invalid!!
{
_exchangeService.Create(server);
}
return Json(new[] { server }.ToDataSourceResult(request, ModelState));
}
and replace model with viewModel ,because in viewModel we don`t have ID (refer to Use ViewModel)
then we will have :
[AcceptVerbs("Post")]
public ActionResult EditingInline_Create([DataSourceRequest] DataSourceRequest request, ViewModel.ServerViewModel server)
{
if (server != null && ModelState.IsValid)
{
_exchangeService.Create(server);
}
return Json(new[] { server }.ToDataSourceResult(request, ModelState));
}

NHibernate HasManyToMany automapping not populating list on load

I've got a many-to-many relationship set up via AutoMapping. Now, the save and updates work fine as expected, however the DiscountGroups are not being loaded in DiscountDay when I get the entities afterwards. I cannot for the life of me work out why the _discountGroups list is always empty, even though it's all correct in the database.
I've seen suggestions about using ISet rather than IList however it doesn't seem to make any difference in my case, neiter does using
.Not.LazyLoad()
in the mapping. Removing AsBag() and AsSet() also makes no difference.
The Entities
public class DiscountDay
{
public virtual DayOfWeek DayOfWeek { get; set; }
public virtual Discount Discount { get; set; }
private readonly IList<DiscountGroup> _discountGroups = new List<DiscountGroup>();
public virtual IEnumerable<DiscountGroup> DiscountGroups
{
get { return _discountGroups; }
set { }
}
}
public class DiscountGroup
{
public virtual string Name { get; set; }
private readonly IList<DiscountDay> _discountDay = new List<DiscountDay>();
public virtual IEnumerable<DiscountDay> DiscountDay
{
get { return _discountDay; }
}
}
The Mappings
public class DiscountDayOverride : IAutoMappingOverride<DiscountDay>
{
public void Override(AutoMapping<DiscountDay> mapping)
{
mapping.HasManyToMany( x => x.DiscountGroups )
.AsSet()
.Cascade
.SaveUpdate();
mapping.Cache.ReadWrite();
}
}
public class DiscountGroupOverride : IAutoMappingOverride<DiscountGroup>
{
public void Override(AutoMapping<DiscountGroup> mapping)
{
mapping.HasManyToMany( x => x.DiscountDay )
.AsBag()
.Inverse();
mapping.Cache.ReadWrite();
}
}
Well, I'm a complete and utter numpty. The empty set on DiscountGroups on the DiscountDay entity was causing the issue. For some reason I'd glossed over it and just didn't think that an empty set would do anything.
Updated code:
public virtual IEnumerable<DiscountGroup> DiscountGroups
{
get { return _discountGroups; }
}

Retrieve a complex object from ActionParameters

I am working on an MVC project where controller actions deal with Assets. Different controllers take in the assetId parameter in different way: Some controllers simply get int assetId, other int id, and other using a complex object AssetDTO dto (which contains a property that holds the assetId)
I am writing an ActionFilter that is added to the action method and is provided with the actionParameter name where I can get the asset value.
Action Method:
[AssetIdFilter("assetId")]
public ActionResult Index(int assetId)
{
...
}
The attribute is defined as:
public class AssetIdFilterAttribute : ActionFilterAttribute
{
public string _assetIdParameterKey { get; set; }
public AssetIdFilterAttribute (string assetIdParameterKey)
{
_assetIdParameterKey = assetIdParameterKey;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
int assetId;
if (Int32.TryParse(filterContext.ActionParameters[_assetIdParameterKey].ToString(), out assetId))
{
......
}
}
This works as expected, but will only work when the assetId is provided as a primitive. I am not sure what to do when the assetId is provided within a complex object into the action method.
Will I need to parse each object differently depending on the type? I am hoping I can specify some kind of dot-notation in the AssetIdFilter to tell it where the assetId is located: dto.assetId
Any way I can use dynamics? or reflection?? ect.???
and here dynamic comes to the rescue.you can change the actionFilterAttribute to be :
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
dynamic assetIdHolder = filterContext.ActionParameters[_assetIdParameterKey];
if (assetIdHolder.GetType().IsPrimitive)
{
//do whatever with assetIdHolder
}
else
{
//do whatever with assetIdHolder.assetId
}
}
cheers!
Well, yes, you answered your question. One way would be to use dot notation:
//simple case:
[AssetId("id")]
public ActionResult Index(string id) {
//code here
}
//complex case:
[AssetId("idObj", AssetIdProperty = "SubObj.id")]
public ActionResult index(IdObject idObj) {
//code here
}
And AssetIdAttribute is as follows:
public class AssetIdAttribute : ActionFilterAttribute
{
public string _assetIdParameterKey { get; set; }
public string AssetIdProperty { get; set; }
public AssetIdFilterAttribute(string assetIdParameterKey)
{
_assetIdParameterKey = assetIdParameterKey;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
int assetId;
var param = filterContext.ActionParameters[_assetIdParameterKey];
int.TryParse(GetPropertyValue(param, this.AssetIdProperty).ToString(), out assetId);
//you code continues here.
}
private static string GetPropertyValue(object souce, string property)
{
var propNames = string.IsNullOrWhiteSpace(property) || !property.Contains('.') ? new string[] { } : property.Split('.');
var result = souce;
foreach (var prop in propNames)
{
result = result.GetType().GetProperty(prop).GetValue(result);
}
return result.ToString();
}
}
The code does not have null checks when calling ToString and when calling GetProperty though. Also, it does not check the success of TryParse. Please apply these corrections when used.
Maybe this code could be written using dynamic, but at the end dynamic usage is compiled into object using reflection (something like what I have done here), thus no big difference to me.
Also, maybe it would be more clear to have a parameter like "idObj.SubObj.id", but that again depends on the preference, and the code will become a little bit more complex.

Implementing Unique Contraint with ValidateEntity gives "The given key was not present in the dictionary" error

While in search of trying to implement unique key validations for my db using EF CodeFirst/Mvc3 I came upon this post http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx which gave an example on how to do it by using IValidateObject for my object model:
public class Category : IValidatableObject
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var testContext = (TestContext)validationContext.Items["Context"];
if (testContext.Categories.Any(
c => c.CategoryName == CategoryName && c.CategoryID != CategoryID))
{
yield return new ValidationResult("A category with the same name already exists!", new[] { "CategoryName" });
}
yield break;
}
}
and overriding DbEntityValidationResult ValidateEntity:
public class TestContext : DbContext
{
public DbSet<Test.Models.Category> Categories { get; set; }
protected override DbEntityValidationResult ValidateEntity( DbEntityEntry entityEntry, IDictionary<object, object> items)
{
var myItems = new Dictionary<object, object>();
myItems.Add("Context", this);
return base.ValidateEntity(entityEntry, myItems);
}
}
And the action on the controller
[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid) {
categoryRepository.InsertOrUpdate(category);
categoryRepository.Save();
return RedirectToAction("Index");
} else {
return View();
}
}
But I get the error: "The given key was not present in the dictionary." for the line
var testContext = (TestContext)validationContext.Items["Context"];
It seems like Validate on the object is getting called which accesses "Context" before its set in the override ValidateEntity code.
At first I thought it could have been ModelState.Isvalid triggering validate too early but it wasn't.
Anyone know what I'm missing here or what I'm doing wrong? Thanks in advance.
Model.IsValid definitely triggers it too early and perhaps something else. IValidatableObject is global interface used by both MVC and EF but your method in DbContext is called only when you call SaveChanges on the context so any usage of IValidatableObject prior to calling SaveChanges will result in the exception. You must use another approach if you want to validate your entity this way. For example store context in HttpContext.Items - you can create custom action filter and instantiate and store the context before the operation call and dispose it after operation call - hopefully it will cover all problems.
I was facing the same problem... Then after a lot of Googling I finally found this:
Exercise 3: Using IValidatableObject Custom Validation
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
MusicStoreEntities storeDB = new MusicStoreEntities();
if (storeDB.Albums.Any(
a => a.Title.Trim().ToUpper() == this.Title.Trim().ToUpper() &&
a.ArtistId == (int)this.ArtistId))
{
yield return new ValidationResult("Existing Album", new string[] { "Title" });
}
}
As you see in their example, they instantiate a new Context and as such there's no need for validationContext.Items["Context"];. Doing so we won't get this error anymore.

Resources