0
votes

I have a table containing AD users info inside my database. Some of the table properties will be updated using Sync job (such as first name, last name, email address,, etc), while other will be added by users inside my system such as Primary Role, Secondary role, OverAll Performance, etc. I have set these values as Required inside my model class, while they allow nulls inside the DB:-

public class SyncADUsers
{
    [Required]
    [StringLength(200)]
    public string PrimaryRole { get; set; }

    [StringLength(250)]
    public string SecondaryRole { get; set; }

    [Required]
    public int OverAllPerfomance { get; set; }

    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

}

But the problem is that when the Sync job start it will raise a validation error that Primary Role, Secondary role, OverAllPerformance are required for any new user that is added to the AD and not yet added to my system. But I need these field to be required only when users try to update the users info inside my system , but to by pass this validation when the sync job start working (especially on new users that are added to the AD , and not yet added to my system database..) Can anyone advice on this? Thanks

1
This doesn't make sense to me - what does the sync job have to do with your MVC view models? - Ant P
Sorry seems that i did not exaplin my question well. now the Sync job will update the model class properties which are retrived from the AD; such as FirstName,LastName,EmailAddress,GUID - john Gu

1 Answers

1
votes

The best option to solve your problem is to create separate models based on the real database model.

I will show you what I meant by one easy example:

In my database user model I have: username, password and name, let's say.

When someone wants to register, I need the user to input all these values, meaning I can use the database model into the view and controller, and all those properties will have [Required] data annotation (MVC). And this will work just perfectly, right?

But, when user wants to log in, we don't need the name property. And that's a problem if we try to use the same database user model as when user is registering. It will show us that the model state (MVC) is not valid, cause we have [Required] on property name, but we don't let user input that while signing in.

Solution: create new model that will be used only for the sign in view, for example. That model will keep only the username and the password from the user. Than you can query the database and check if the username/password combination is correct and so on.