0

I have an account field, which has a dropdown box of accounts, which underlying, sends off the account number.

So when nothing is entered, the validation message says "The field AccNo must be a number." or "The AccNo field is required." The default option sometimes triggers the number type validation as it has no number assigned.

Is there any way I can change this to simply say, "Please select an Account", for cases where nothing is entered AND when it assumes a number isnt entered.

2 Answers 2

2

Sure, you could decorate the property you are binding your dropdownlist to with the [Required] attribute which will enforce the user to select a value:

public class AccountViewModel
{
    [Required(ErrorMessage = "Please Select an Account")]
    public string AccountNumber { get; set; }

    public IEnumerable<SelectListItems> Accounts { get; set; }
}

and in the view:

@model AccountViewModel
...
@Html.DropDownListFor(
    x => x.AccountNumber,
    Model.Accounts,
    "-- select an account --"
)
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure that I understand you completely, but can't you just use the annotation:

[Range(0, 99999,ErrorMessage = "Please select an Account")]

on the field in your model ?
(I used the Range validation only for the example)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.