Generic Type Generic and BindAttribute

I am having a problem with bind attribute in asp.net mvc. I have a custom ViewModel that looks like this:

public interface IUserView<TUser> where TUser : User
{
    TUser User { get; set; }
    string Email { get; set; }
    string ConfirmEmail { get; set; }
    string Password { get; set; }
    string ConfirmPassword { get; set; }
}
public class EditUserView<TUser> : IUserView<TUser> where TUser : User
{
    public virtual TUser User { get; set; }

    [ValidateRegExp(RegexConstants.Email, "Invalid Email.")]
    [ValidateNonEmpty("email is required.")]
    public virtual string Email { get; set; }

    [ValidateSameAs("Email", "confirmation email does not match.")]
    public virtual string ConfirmEmail { get; set; }


    public virtual string Password { get; set; }

    [ValidateSameAs("Password", "confirmation password does not match.")]
    public virtual string ConfirmPassword { get; set; }
}

      

and a method for sending this value, which looks like this:

public ActionResult SubmitProfile([Bind(Exclude="IsSystemAdmin")]EditUserView<Admin> iuserview)

      

if you look at the above method you will see the bind attribute with "Exclude =" IsSystemAdmin ". The admin model has a boolean property named" IsSystemAdmin "which elevates their permission. Now obviously I don't want the admin to be able to do yourself as a system administrator by simply returning the true value for this field.

I tried both "IsSystemAdmin" and "User.IsSystemAdmin" properties in the exclude property and neither of them stop updating the IsSystemAdmin variable. Is there a way to make the binding attribute work in this scenario or is it a bug in the default binding object?

0


a source to share


1 answer


Have you tried using the Bind attribute on the TUser class itself

[Bind(Exclude = "IsSysAdmin")]
public class TUser 
{

}

      



I think that due to the importance of this property, you will never want this to be set with a binding object, and hence binding to a class will be fine. Most likely you are bumping administrator up to sysadmin by explicitly setting a flag for the action.

I am very wary of binding any model classes directly from form messages. I have special form classes for binding and update model classes in my repository level only through a different service level.

+1


a source







All Articles