Int or Number DataType for DataAnnotation validation attribute

前端 未结 8 2435
我寻月下人不归
我寻月下人不归 2020-12-07 10:41

On my MVC3 project, I store score prediction for football/soccer/hockey/... sport game. So one of properties of my prediction class looks like this:

[Range(0         


        
8条回答
  •  醉梦人生
    2020-12-07 10:44

    I was able to bypass all the framework messages by making the property a string in my view model.

    [Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")]
    [StringLength(2, ErrorMessage = "Max 2 digits")]
    [Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")]
    public string HomeTeamPrediction { get; set; }
    

    Then I need to do some conversion in my get method:

    viewModel.HomeTeamPrediction = databaseModel.HomeTeamPrediction.ToString();
    

    and post method:

    databaseModel.HomeTeamPrediction = int.Parse(viewModel.HomeTeamPrediction);
    

    This works best when using the range attribute, otherwise some additional validation would be needed to make sure the value is a number.

    You can also specify the type of number by changing the numbers in the range to the correct type:

    [Range(0, 10000000F, ErrorMessageResourceType = typeof(GauErrorMessages), ErrorMessageResourceName = nameof(GauErrorMessages.MoneyRange))]
    

提交回复
热议问题