public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            try
            {
                PhoneNumber phoneNumber = _phoneUtil.Parse(PhoneNumberCheck.PhoneNumberRaw, PhoneNumberCheck.CountryCodeSelected);

                var valid = _phoneUtil.IsValidNumberForRegion(phoneNumber, PhoneNumberCheck.CountryCodeSelected);
                Debug.Print($"{phoneNumber} is {valid} for region {PhoneNumberCheck.CountryCodeSelected}");

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.CountryCodeSelected)}").Value.RawValue =
                    PhoneNumberCheck.CountryCodeSelected;

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.PhoneNumberRaw)}").Value.RawValue =
                    PhoneNumberCheck.PhoneNumberRaw;

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.Valid)}").Value.RawValue =
                    _phoneUtil.IsValidNumberForRegion(phoneNumber, PhoneNumberCheck.CountryCodeSelected);

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.PhoneNumberType)}").Value.RawValue =
                    _phoneUtil.GetNumberType(phoneNumber);

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.RegionCode)}").Value.RawValue =
                    _phoneUtil.GetRegionCodeForNumber(phoneNumber);

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.PhoneNumberFormatted)}").Value.RawValue =
                    _phoneUtil.FormatOutOfCountryCallingNumber(phoneNumber, PhoneNumberCheck.CountryCodeSelected);

                ModelState.FirstOrDefault(x => x.Key == $"{nameof(PhoneNumberCheck)}.{nameof(PhoneNumberCheck.PhoneNumberMobileDialing)}").Value.RawValue =
                    _phoneUtil.FormatNumberForMobileDialing(phoneNumber, PhoneNumberCheck.CountryCodeSelected, true);

                return(Page());
            }
            catch (NumberParseException npex)
            {
                ModelState.AddModelError(npex.ErrorType.ToString(), npex.Message);
            }
            return(Page());
        }
Пример #2
0
        public IActionResult Check(PhoneNumberCheckViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentException(nameof(model));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    PhoneNumber phoneNumber = _phoneUtil.Parse(model.PhoneNumberRaw, model.CountryCodeSelected);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.Valid)).Value.RawValue =
                        _phoneUtil.IsValidNumberForRegion(phoneNumber, model.CountryCodeSelected);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberType)).Value.RawValue =
                        _phoneUtil.GetNumberType(phoneNumber);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.CountryCode)).Value.RawValue =
                        _phoneUtil.GetRegionCodeForNumber(phoneNumber);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberFormatted)).Value.RawValue =
                        _phoneUtil.FormatOutOfCountryCallingNumber(phoneNumber, "US");
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberMobileDialing)).Value.RawValue =
                        _phoneUtil.FormatNumberForMobileDialing(phoneNumber, model.CountryCodeSelected, true);

                    ModelState.FirstOrDefault(x => x.Key == nameof(model.HasExtension)).Value.RawValue =
                        phoneNumber.HasExtension;

                    ModelState.FirstOrDefault(x => x.Key == nameof(model.CountryCodeSelected)).Value.RawValue =
                        model.CountryCodeSelected;
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberRaw)).Value.RawValue =
                        model.PhoneNumberRaw;

                    model.Countries = _countries.CountrySelectList;

                    return(View(model));
                }
                catch (NumberParseException npex)
                {
                    ModelState.AddModelError(npex.ErrorType.ToString(), npex.Message);
                }
            }

            model.Countries = _countries.CountrySelectList;

            ModelState.SetModelValue(nameof(model.CountryCodeSelected), model.CountryCodeSelected, model.CountryCodeSelected);
            ModelState.SetModelValue(nameof(model.PhoneNumberRaw), model.PhoneNumberRaw, model.PhoneNumberRaw);

            ModelState.SetModelValue(nameof(model.Valid), false, null);
            model.Valid = false;
            ModelState.SetModelValue(nameof(model.HasExtension), false, null);
            model.HasExtension = false;
            ModelState.SetModelValue(nameof(model.PhoneNumberType), null, null);
            model.PhoneNumberType = null;
            ModelState.SetModelValue(nameof(model.CountryCode), null, null);
            model.CountryCode = null;
            ModelState.SetModelValue(nameof(model.PhoneNumberFormatted), null, null);
            model.PhoneNumberFormatted = null;
            ModelState.SetModelValue(nameof(model.PhoneNumberMobileDialing), null, null);
            model.PhoneNumberMobileDialing = null;

            return(View(model));
        }
Пример #3
0
        public IActionResult Check(PhoneNumberCheckViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    // Parse the number to check into a PhoneNumber object.
                    PhoneNumber phoneNumber = _phoneUtil.Parse(model.PhoneNumberRaw, model.CountryCodeSelected);

                    // Use the PhoneNumber object to get information from the utility and assign it to the raw state of the model.
                    // The values can't be assigned directly to the model because they have previously existing models.
                    // ASP.NET Core Tag Helpers work differently than Html Helpers in this respect (and others).
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.Valid)).Value.RawValue =
                        _phoneUtil.IsValidNumberForRegion(phoneNumber, model.CountryCodeSelected);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberType)).Value.RawValue =
                        _phoneUtil.GetNumberType(phoneNumber);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.CountryCode)).Value.RawValue =
                        _phoneUtil.GetRegionCodeForNumber(phoneNumber);
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberFormatted)).Value.RawValue =
                        _phoneUtil.FormatOutOfCountryCallingNumber(phoneNumber, "US");
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberMobileDialing)).Value.RawValue =
                        _phoneUtil.FormatNumberForMobileDialing(phoneNumber, model.CountryCodeSelected, true);

                    ModelState.FirstOrDefault(x => x.Key == nameof(model.HasExtension)).Value.RawValue =
                        phoneNumber.HasExtension;

                    // The submitted value has to be returned as the raw value.
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.CountryCodeSelected)).Value.RawValue =
                        model.CountryCodeSelected;
                    ModelState.FirstOrDefault(x => x.Key == nameof(model.PhoneNumberRaw)).Value.RawValue =
                        model.PhoneNumberRaw;

                    // Because the Countries property of the view model doesn't exist at this point (it's not passed back by
                    // the model binder when the form is submitted) it can be assigned directly before being returned to the view.
                    model.Countries = _countries.CountrySelectList;

                    return(View(model));
                }
                catch (NumberParseException npex)
                {
                    // If PhoneNumberUtil throws an error, add it to the list of ModelState errors.
                    // This will change ModelState.IsValid to false.
                    ModelState.AddModelError(npex.ErrorType.ToString(), npex.Message);
                }
            }

            // If there is an unspecified ModelState error or a NumberParseException
            // repopulate the list of countries, selected country, and attempted phone number.
            // Clear the values of the results in both the ModelState values and the model.
            // Doing both is required by MVC handling of Tag Helpers. For Html Helpers, only
            // the model values need to be reset.
            // In production code, the results section would best be implemented with a partial
            // page with its own view model, which could then be reinitialized for each new
            // attempt. It's handled this way to keep the focus on the library functionality.

            model.Countries = _countries.CountrySelectList;

            ModelState.SetModelValue(nameof(model.CountryCodeSelected), model.CountryCodeSelected, model.CountryCodeSelected);
            ModelState.SetModelValue(nameof(model.PhoneNumberRaw), model.PhoneNumberRaw, model.PhoneNumberRaw);

            ModelState.SetModelValue(nameof(model.Valid), false, null);
            model.Valid = false;
            ModelState.SetModelValue(nameof(model.HasExtension), false, null);
            model.HasExtension = false;
            ModelState.SetModelValue(nameof(model.PhoneNumberType), null, null);
            model.PhoneNumberType = null;
            ModelState.SetModelValue(nameof(model.CountryCode), null, null);
            model.CountryCode = null;
            ModelState.SetModelValue(nameof(model.PhoneNumberFormatted), null, null);
            model.PhoneNumberFormatted = null;
            ModelState.SetModelValue(nameof(model.PhoneNumberMobileDialing), null, null);
            model.PhoneNumberMobileDialing = null;

            return(View(model));
        }