Say I have a viewmodel like so:
public class User
{
public int Id { get; set; }
[Required(ErrorMessage="Username is required")]
public string Username { get; set; }
[Range(0, 255)]
public int Owner { get; set; }
}
The page is submitted to it's controller where I check if ModelState.IsValid, however it doesn't pass. Obviously, owner is required. Why is that? I thought the default value for an unassigned int was 0. If I debug the app and inspect the object that is sent to the controller, the value is indeed 0.
If I don't want to force the user to enter 0, what's the best approach? I've tried adding a [DefaultValue(0)] attribute to the Owner property in the class but it does not seem to do any difference.
Some guidance would be nice even if this is such a newbie or trivial getting-used-to-the-concept kind of problem.
Regards,
Did you try making the Owner int
a nullable value?
[Range(0, 255)]
public int? Owner { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With