コード例 #1
0
        public IActionResult Country(CountryVM countryVM)
        {
            CountryVM_DataManager countryVM_DataManager = new CountryVM_DataManager();
            ModelStateDictionary  msd = CountryVM_DataManager.ValidateCountry(ref countryVM);

            //Model validation occurs prior to each controller action being invoked, and it's the action method's responsibility to inspect ModelState.IsValid and react appropriately.


            //Manual validation
            //After model binding and validation are complete, you may want to repeat parts of it. For example, a user may have entered text in a field expecting an integer, or you may need to compute a value for a model's property.
            //You may need to run validation manually.To do so, call the TryValidateModel

            //when TryUpdateModel()  is called it doesnot raise exceptions you should use ValidateModel Or TryValidateModel
            bool b = TryValidateModel(countryVM);

            foreach (string K in msd.Keys)
            {
                ModelStateEntry mse = null;
                msd.TryGetValue(K, out mse);
                ModelState.AddModelError(K, mse.Errors[0].ErrorMessage);
            }
            bool c = ModelState.IsValid;


            if (!c)
            {
                return(View(countryVM));
            }


            countryVM_DataManager.SaveCountry(countryVM);
            return(RedirectToAction("Country"));
        }
コード例 #2
0
        public IActionResult Country()
        {
            CountryVM_DataManager countryVM_DataManager = new CountryVM_DataManager();

            return(View(countryVM_DataManager.countryVM));
        }