0

Again me with another problem. I have problem like this: Non-static method requires a target

So, I created my validation and it works normal when I use it directly in Create View but I need it also not just in create. Long story short this is my validation.

public class EmailUpisaniAttribute : ValidationAttribute
{ 

    private ApplicationDbContext db = new ApplicationDbContext();
    public override bool IsValid(object value)
    {
        if (value is VMUpi VMUpi)
        {
            var pre = db.Upi.FirstOrDefault(x=>x.Email == VMUpi.Upi.Email && x.Upi_ID != VMUpi.Upi.Upi_ID);
            if (pre == null)
            {
                return true;
            }
        }
        return false;
    }

}

So, on that link that I posted they say its probably because of lambda in where but I dont know what can I do to solve this problem.

dee-see
  • 23,668
  • 5
  • 58
  • 91
  • Sometimes error messages in EF (I suppose?) queries can be very tough to understand. Try changing `db.Upi.FirstOrDefault...` to `db.Upi.ToList().FirstOrDefault` and see if you get a better error message when executing the lambda on a materialized query. – dee-see Aug 02 '19 at 00:11
  • TY. I have and now it says System.NullReferenceException: 'Object reference not set to an instance of an object.' Its pointing on x.Email – dsad sdasdas Aug 02 '19 at 00:14
  • 1
    Careful that you're not traversing children and assuming they're not null when calling children of children. For example VMUpi.Upi.Email. Upi could be null and you're calling Email – Patrick Goode Aug 02 '19 at 00:23
  • @dsadsdasdas then you're going through exactly the same thing as the referenced question. You should add a null check for `VMUpi.Upi – dee-see Aug 02 '19 at 12:17
  • Possible duplicate of [Non-static method requires a target](https://stackoverflow.com/questions/13717355/non-static-method-requires-a-target) – dee-see Aug 02 '19 at 12:19
  • Its not null. I checked. If you want I can post picture. ONly what is null are IDs. – dsad sdasdas Aug 02 '19 at 13:21

1 Answers1

0

Now I know what is the problem. When I press the button submit it first checks custom validations and then goes in controller for post method. Problem is that My IDs are 0 when it goes in validation. IDs became number after it goes in post method but my model is already false. Is there any solution for that.