public IEnumerable <KeyValuePair <string, string> > ToUrlEncodedContent(CreateSessionViewModel viewModel) { var kvPairs = new List <KeyValuePair <string, string> >(); var allProps = viewModel.GetType().GetProperties(); foreach (var propertyInfo in allProps) { kvPairs.Add(new KeyValuePair <string, string>(propertyInfo.Name, viewModel.GetType().GetProperty(propertyInfo.Name).GetValue(viewModel)?.ToString() ?? String.Empty)); } return(kvPairs); }
public void CreateSessionWithInvalidModel_ShouldReturnModelStateNotValid() { var controller = new SessionController(_activityManager, _sessionManager, _personManager, _utilityManager); var model = new CreateSessionViewModel() { Activity = null, Name = "" }; //Init ModelState var modelBinder = new ModelBindingContext() { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType( () => model, model.GetType()), ValueProvider = new NameValueCollectionValueProvider( new NameValueCollection(), CultureInfo.InvariantCulture) }; var binder = new DefaultModelBinder().BindModel( new ControllerContext(), modelBinder); controller.ModelState.Clear(); controller.ModelState.Merge(modelBinder.ModelState); ViewResult result = (ViewResult)controller.CreateSession(model); Assert.IsTrue(result.ViewData.ModelState["Activity"].Errors.Count > 0); Assert.IsTrue(result.ViewData.ModelState["Name"].Errors.Count > 0); Assert.IsTrue(!result.ViewData.ModelState.IsValid); }