private CommonQueryCriteria DesrializeCommonQueryCriteria(IValueProvider values) { var criteria = new CommonQueryCriteria(); if (values.ContainsPrefix("$orderby")) { var value = values.GetValue("$orderby"); criteria.OrderBy = value.AttemptedValue; } if (values.ContainsPrefix("$pageNumber")) { var pageNumber = (int)values.GetValue("$pageNumber").ConvertTo(typeof(int)); int pageSize = 10; if (values.ContainsPrefix("$pageSize")) { pageSize = (int)values.GetValue("$pageSize").ConvertTo(typeof(int)); } var pagingInfo = new PagingInfo(pageNumber, pageSize); criteria.PagingInfo = pagingInfo; } //filter //var jFilter = json.Property("$filter"); //if (jFilter != null) //{ // var filter = jFilter.Value.Value<string>(); // ParseFilter(criteria, filter); //} return(criteria); }
public void GetValueProvider_SimpleArrayJsonObject() { const string jsonString = @" [ ""abc"", null, ""foobar"" ] "; ControllerContext cc = GetJsonEnabledControllerContext(jsonString); JsonValueProviderFactory factory = new JsonValueProviderFactory(); // Act & assert IValueProvider valueProvider = factory.GetValueProvider(cc); Assert.True(valueProvider.ContainsPrefix("[0]")); Assert.True(valueProvider.ContainsPrefix("[2]")); Assert.False(valueProvider.ContainsPrefix("[3]")); ValueProviderResult vpResult1 = valueProvider.GetValue("[0]"); Assert.Equal("abc", vpResult1.AttemptedValue); Assert.Equal(CultureInfo.CurrentCulture, vpResult1.Culture); // null values should exist in the backing store as actual entries ValueProviderResult vpResult2 = valueProvider.GetValue("[1]"); Assert.NotNull(vpResult2); Assert.Null(vpResult2.RawValue); }
public bool ContainsPrefix(string prefix) { if (prefix == _alias.OriginalPropertyName) { return(_provider.ContainsPrefix(_alias.OriginalPropertyName) || _provider.ContainsPrefix(_alias.AliasName)); } return(_provider.ContainsPrefix(prefix)); }
public void GetValueProvider() { // Arrange Dictionary <string, object> backingStore = new Dictionary <string, object>( StringComparer.OrdinalIgnoreCase ) { { "foo", "fooValue" }, { "bar.baz", "barBazValue" } }; MockSessionState session = new MockSessionState(backingStore); Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.Setup(o => o.HttpContext.Session).Returns(session); SessionValueProviderFactory factory = new SessionValueProviderFactory(); // Act IValueProvider provider = factory.GetValueProvider(mockControllerContext.Object); // Assert Assert.True(provider.ContainsPrefix("bar")); Assert.Equal("fooValue", provider.GetValue("foo").AttemptedValue); Assert.Equal(CultureInfo.InvariantCulture, provider.GetValue("foo").Culture); }
private void BindCollection(ModelStateDictionary modelState, IValueProvider valueProvider, string modelName, DataSet dataSet) { var indexNames = GetIndexNames(valueProvider, modelName); bool indexNamesIsFinite; if (indexNames != null) { indexNamesIsFinite = true; } else { indexNamesIsFinite = false; indexNames = Enumerable.Range(0, int.MaxValue).Select(i => i.ToString(CultureInfo.InvariantCulture)); } var currentIndex = 0; foreach (var indexName in indexNames) { var dataRowModelName = ModelNames.CreateIndexModelName(modelName, indexName); if (!valueProvider.ContainsPrefix(dataRowModelName) && !indexNamesIsFinite) { break; } if (currentIndex >= dataSet.Count) { dataSet.AddRow(); } Bind(modelState, valueProvider, dataRowModelName, dataSet[currentIndex++]); } }
public void GetValueProvider() { // Arrange NameValueCollection serverVars = new NameValueCollection { { "foo", "fooValue" }, { "bar.baz", "barBazValue" } }; Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext .Setup(o => o.HttpContext.Request.ServerVariables) .Returns(serverVars); ServerVariablesValueProviderFactory factory = new ServerVariablesValueProviderFactory(); // Act IValueProvider provider = factory.GetValueProvider(mockControllerContext.Object); // Assert Assert.True(provider.ContainsPrefix("bar")); Assert.Equal("fooValue", provider.GetValue("foo").AttemptedValue); Assert.Equal(CultureInfo.InvariantCulture, provider.GetValue("foo").Culture); }
protected virtual object GetDictionaryModel(ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string prefix) { List<KeyValuePair<object, object>> list = new List<KeyValuePair<object, object>>(); bool numericIndex; IEnumerable<string> indexes = GetIndexes(prefix, valueProvider, out numericIndex); Type[] genericArguments = modelType.GetGenericArguments(); Type keyType = genericArguments[0]; Type valueType = genericArguments[1]; foreach (var index in indexes) { string indexPrefix = prefix + "[" + index + "]"; if (!valueProvider.ContainsPrefix(indexPrefix) && numericIndex) { break; } string keyPrefix = indexPrefix + ".Key"; string valulePrefix = indexPrefix + ".Value"; object key = GetModel(controllerContext, keyType, valueProvider, keyPrefix); object value = GetModel(controllerContext, valueType, valueProvider, valulePrefix); list.Add(new KeyValuePair<object, object>(key, value)); } object model = CreateModel(modelType); ReplaceHelper.ReplaceDictionary(keyType, valueType, model, list); return model; }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string prefix = bindingContext.ModelName; IValueProvider valueProvider = bindingContext.ValueProvider; bool containsPrefix = valueProvider.ContainsPrefix(prefix); //如果ValueProvider的数据容器中不包含指定前缀的数据 //并且启用“去除前缀后的二次绑定”,会将ModelName设置为Null if (!containsPrefix) { if (!bindingContext.FallbackToEmptyPrefix) { return(null); } bindingContext.ModelName = null; } else { //采用针对简单类型的数据绑定 ValueProviderResult valueProviderResult = valueProvider.GetValue(prefix); if (null != valueProviderResult) { return(this.BindSimpleModel(controllerContext, bindingContext, valueProviderResult)); } } if (bindingContext.ModelMetadata.IsComplexType) { //采用针对复杂类型的数据绑定 return(this.BindComplexModel(controllerContext, bindingContext)); } return(null); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { IValueProvider provider = bindingContext.ValueProvider; // Check whether we have a list or a single object. If we have a list // then all the properties will be prefixed with an index in square brackets. if (provider.ContainsPrefix("[0]")) { // We have a list. Since that is what the controller method is // expecting, just use the default model binder to do the work for us. return(ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, bindingContext)); } else { // We have a single object. // Bind it manually and return it in a list. ClassA a = new ClassA(); a.ID = GetValue <int>(provider, "ID"); a.FirstName = GetValue <string>(provider, "FirstName"); a.LastName = GetValue <string>(provider, "LastName"); return(new List <ClassA> { a }); } }
public object GetModel(ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string key) { if (!valueProvider.ContainsPrefix(key)) { return null; } ModelMetadata modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, modelType); if (!modelMetadata.IsComplexType) { return valueProvider.GetValue(key).ConvertTo(modelType); } if (modelType.IsArray) { return GetArrayModel(controllerContext, modelType, valueProvider,key); } Type enumerableType = ExtractGenericInterface(modelType, typeof(IEnumerable<>)); if (null != enumerableType) { return GetCollectionModel(controllerContext, modelType, valueProvider, key); } if (modelMetadata.IsComplexType) { return GetComplexModel(controllerContext, modelType, valueProvider, key); } return null; }
public void GetValueProvider() { // Arrange HttpCookieCollection cookies = new HttpCookieCollection { new HttpCookie("foo", "fooValue"), new HttpCookie("bar.baz", "barBazValue"), new HttpCookie("", "emptyValue"), new HttpCookie(null, "nullValue") }; Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.Setup(o => o.HttpContext.Request.Cookies).Returns(cookies); CookieValueProviderFactory factory = new CookieValueProviderFactory(); // Act IValueProvider provider = factory.GetValueProvider(mockControllerContext.Object); // Assert Assert.Null(provider.GetValue("")); Assert.True(provider.ContainsPrefix("bar")); Assert.Equal("fooValue", provider.GetValue("foo").AttemptedValue); Assert.Equal(CultureInfo.InvariantCulture, provider.GetValue("foo").Culture); }
public bool ContainsPrefix(string prefix) { if (prefix == _originalName) { return(_allNamesToBind.Any(_provider.ContainsPrefix)); } return(_provider.ContainsPrefix(prefix)); }
public object GetModel(ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string key) { if (!valueProvider.ContainsPrefix(key)) { return null; } return valueProvider.GetValue(key).ConvertTo(modelType); }
public void GetValueProvider_ComplexJsonObject() { // Arrange const string jsonString = @" [ { ""BillingAddress"": { ""Street"": ""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZIP"": 98052 }, ""ShippingAddress"": { ""Street"": ""123 Anywhere Ln"", ""City"": ""Anytown"", ""State"": ""ZZ"", ""ZIP"": 99999 } }, { ""Enchiladas"": [ ""Delicious"", ""Nutritious""] } ] "; ControllerContext cc = GetJsonEnabledControllerContext(jsonString); JsonValueProviderFactory factory = new JsonValueProviderFactory(); // Act & assert IValueProvider valueProvider = factory.GetValueProvider(cc); Assert.NotNull(valueProvider); Assert.True(valueProvider.ContainsPrefix("[0].billingaddress")); Assert.Null(valueProvider.GetValue("[0].billingaddress")); Assert.True(valueProvider.ContainsPrefix("[0].billingaddress.street")); Assert.NotNull(valueProvider.GetValue("[0].billingaddress.street")); ValueProviderResult vpResult1 = valueProvider.GetValue("[1].enchiladas[0]"); Assert.NotNull(vpResult1); Assert.Equal("Delicious", vpResult1.AttemptedValue); Assert.Equal(CultureInfo.CurrentCulture, vpResult1.Culture); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string key = bindingContext.ModelName; IValueProvider valueProvider = bindingContext.ValueProvider; if (valueProvider.ContainsPrefix(bindingContext.ModelName)) { ValueProviderResult valueProviderResult = valueProvider.GetValue(key); return(this.BindSimpleModel(controllerContext, bindingContext, valueProviderResult)); } return(null); }
protected virtual object GetArrayModel( ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string prefix) { if (valueProvider.ContainsPrefix(prefix) && !string.IsNullOrEmpty(prefix)) { ValueProviderResult result = valueProvider.GetValue(prefix); if (null != result) { return result.ConvertTo(modelType); } } return null; }
/// <summary> /// 在表单上应用指定值提供程序的值 /// </summary> /// <param name="form">要应用的表单</param> /// <param name="valueProvider">提供值的 ValueProvider 实例</param> /// <returns>返回表单,便于链式调用</returns> public static HtmlForm ApplyValues( this HtmlForm form, IValueProvider valueProvider ) { foreach ( var key in form.InputControls.Select( c => c.Name ) ) { if ( valueProvider.ContainsPrefix( key ) ) { form[key].TrySetValue( valueProvider.GetValue( key ).AttemptedValue ); } } return form; }
/// <summary> /// 在表单上应用指定值提供程序的值 /// </summary> /// <param name="form">要应用的表单</param> /// <param name="valueProvider">提供值的 ValueProvider 实例</param> /// <returns>返回表单,便于链式调用</returns> public static HtmlForm ApplyValues(this HtmlForm form, IValueProvider valueProvider) { foreach (var key in form.InputControls.Select(c => c.Name)) { if (valueProvider.ContainsPrefix(key)) { form[key].TrySetValue(valueProvider.GetValue(key).AttemptedValue); } } return(form); }
/// <summary> /// 在表单上应用指定值提供程序的值 /// </summary> /// <param name="form">要应用的表单</param> /// <param name="valueProvider">提供值的 ValueProvider 实例</param> /// <returns>返回表单,便于链式调用</returns> public static HtmlForm ApplyValues(this HtmlForm form, IValueProvider valueProvider) { foreach (var control in form.Controls) { if (valueProvider.ContainsPrefix(control.Name)) { control.Value = valueProvider.GetValue(control.Name).AttemptedValue; } } return(form); }
/// <summary> /// 在表单上应用指定值提供程序的值 /// </summary> /// <param name="form">要应用的表单</param> /// <param name="valueProvider">提供值的 ValueProvider 实例</param> /// <returns>返回表单,便于链式调用</returns> public static HtmlForm ApplyValues( this HtmlForm form, IValueProvider valueProvider ) { foreach ( var control in form.Controls ) { if ( valueProvider.ContainsPrefix( control.Name ) ) control.Value = valueProvider.GetValue( control.Name ).AttemptedValue; } return form; }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { DataTablesRequestModel model = ModelBinders.Binders.GetBinder(typeof(DataTablesRequestModel)).BindModel(controllerContext, bindingContext) as DataTablesRequestModel; if (model == null || !model.sEcho.HasValue) { return(null); } IValueProvider valueProvider = bindingContext.ValueProvider; model.ColumnFilters = new List <DataTablesRequestModel.ColumnFilter>(); model.Sorts = new List <DataTablesRequestModel.Sort>(); for (int i = 0; i < model.iColumns && valueProvider.ContainsPrefix(string.Format("bSearchable_{0}", i)); i++) { DataTablesRequestModel.ColumnFilter column = new DataTablesRequestModel.ColumnFilter { bSearchable = GetValue <bool>(valueProvider, "bSearchable_", i), sSearch = GetValue <string>(valueProvider, "sSearch_", i), bRegex = GetValue <bool>(valueProvider, "bRegex_", i), bSortable = GetValue <bool>(valueProvider, "bSortable_", i), mDataProp = GetValue <string>(valueProvider, "mDataProp_", i) }; model.ColumnFilters.Add(column); } for (int i = 0; i < model.iSortingCols && valueProvider.ContainsPrefix(string.Format("iSortCol_{0}", i)); i++) { DataTablesRequestModel.Sort sort = new DataTablesRequestModel.Sort { iSortCol = GetValue <int>(valueProvider, "iSortCol_", i), sSortDir = GetValue <string>(valueProvider, "sSortDir_", i) }; model.Sorts.Add(sort); } return(model); }
public void LoadPostData(IValueProvider data) { foreach (var option in Options) { var optionName = ToOptionName(option.Key); if (!data.ContainsPrefix(optionName)) { continue; } var value = data.GetValue(optionName); option.Value.CurrentValue = (string)value.ConvertTo(typeof(string)); } }
public object GetModel(ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string key) { if (!valueProvider.ContainsPrefix(key)) { return null; } ModelMetadata modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, modelType); if (!modelMetadata.IsComplexType) { return valueProvider.GetValue(key).ConvertTo(modelType); } if (modelMetadata.IsComplexType) { return GetComplexModel(controllerContext, modelType, valueProvider, key); } return null; }
private static TType GetValue <TType>(IValueProvider provider, string keyBase, int index) { string key = keyBase + index; if (!provider.ContainsPrefix(key)) { return(default(TType)); } try { return((TType)Convert.ChangeType(provider.GetValue(key).AttemptedValue, typeof(TType))); } catch (Exception) { return(default(TType)); } }
private static PropertyValue GetPropertyValueForFile( Property property, IValueProvider valueProvider, HttpFileCollectionBase files) { var propertyValue = new PropertyValue(property); var file = files[property.Name]; propertyValue.Raw = file; if (property.TypeInfo.IsFileStoredInDb == false && property.FileOptions.NameCreation == NameCreation.UserInput) { var providedName = (string)valueProvider.GetValue(property.Name) .ConvertTo(typeof(string), CultureInfo.CurrentCulture); propertyValue.Additional = providedName; } var isDeleted = false; if (file == null || file.ContentLength > 0) { isDeleted = false; } else { var isDeletedKey = property.Name + "_delete"; if (valueProvider.ContainsPrefix(isDeletedKey)) { isDeleted = ((bool?) valueProvider.GetValue(isDeletedKey) .ConvertTo(typeof(bool), CultureInfo.CurrentCulture)).GetValueOrDefault(); } } if (isDeleted) { propertyValue.DataBehavior = DataBehavior.Clear; propertyValue.Additional = null; } return(propertyValue); }
public void GetValueProvider_SimpleDictionaryJsonObject() { const string jsonString = @" { ""FirstName"":""John"", ""LastName"": ""Doe"" }"; ControllerContext cc = GetJsonEnabledControllerContext(jsonString); JsonValueProviderFactory factory = new JsonValueProviderFactory(); // Act & assert IValueProvider valueProvider = factory.GetValueProvider(cc); Assert.True(valueProvider.ContainsPrefix("firstname")); ValueProviderResult vpResult1 = valueProvider.GetValue("firstname"); Assert.Equal("John", vpResult1.AttemptedValue); Assert.Equal(CultureInfo.CurrentCulture, vpResult1.Culture); }
public void GetValueProvider_ComplexJsonObject() { // Arrange string jsonString = @" [ { ""FirstName"": ""John"", ""LastName"": ""Doe"", ""Age"": 32, ""BillingAddress"": { ""Street"": ""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZIP"": 98052 }, ""ShippingAddress"": { ""Street"": ""123 Anywhere Ln"", ""City"": ""Anytown"", ""State"": ""ZZ"", ""ZIP"": 99999 } }, { ""Enchiladas"": [ ""Delicious"", ""Nutritious"", null ] } ] "; ControllerContext cc = GetJsonEnabledControllerContext(jsonString); JsonValueProviderFactory factory = new JsonValueProviderFactory(); // Act & assert 1 IValueProvider valueProvider = factory.GetValueProvider(cc); Assert.IsNotNull(valueProvider); // Act & assert 2 Assert.IsTrue(valueProvider.ContainsPrefix("[0].billingaddress"), "[0].billingaddress prefix should have existed."); Assert.IsNull(valueProvider.GetValue("[0].billingaddress"), "[0].billingaddress key should not have existed."); ValueProviderResult vpResult1 = valueProvider.GetValue("[1].enchiladas[0]"); Assert.IsNotNull(vpResult1); Assert.AreEqual("Delicious", vpResult1.AttemptedValue); Assert.AreEqual(CultureInfo.CurrentCulture, vpResult1.Culture); // null values should exist in the backing store as actual entries ValueProviderResult vpResult2 = valueProvider.GetValue("[1].enchiladas[2]"); Assert.IsNotNull(vpResult2); Assert.IsNull(vpResult2.RawValue); }
private void Bind(ModelStateDictionary modelState, IValueProvider valueProvider, string modelName, DataSet dataSet, bool isScalar) { modelState.SetModelValue(modelName, dataSet, string.Empty); if (!string.IsNullOrEmpty(modelName) && !valueProvider.ContainsPrefix(modelName)) { return; } var _ = dataSet.Model; _.SuspendIdentity(); if (isScalar) { BindScalar(modelState, valueProvider, modelName, dataSet); } else { BindCollection(modelState, valueProvider, modelName, dataSet); } _.ResumeIdentity(); }
public bool ContainsPrefix(string prefix) { return(_backingProvider.ContainsPrefix(prefix)); }
private List<object> GetListModel(ControllerContext controllerContext, Type modelType, Type elementType, IValueProvider valueProvider, string prefix) { List<object> list = new List<object>(); if (!string.IsNullOrEmpty(prefix) && valueProvider.ContainsPrefix(prefix)) { ValueProviderResult result = valueProvider.GetValue(prefix); if (null != result) { IEnumerable enumerable = result.ConvertTo(modelType) as IEnumerable; foreach (var value in enumerable) { list.Add(value); } } } bool numericIndex; IEnumerable<string> indexes = GetIndexes(prefix, valueProvider, out numericIndex); foreach (var index in indexes) { string indexPrefix = prefix + "[" + index + "]"; if (!valueProvider.ContainsPrefix(indexPrefix) && numericIndex) { break; } list.Add(GetModel(controllerContext, elementType, valueProvider, indexPrefix)); } return list; }
public bool ContainsPrefix(string prefix) { var result = _valueProvider.ContainsPrefix(prefix); return(result); }
public bool ContainsPrefix(string prefix) { return(prefix == "model" || valueProvider.ContainsPrefix(prefix)); }
private static PropertyValue GetPropertyValueForFile( Property property, IValueProvider valueProvider, HttpFileCollectionBase files) { var propertyValue = new PropertyValue(property); var file = files[property.Name]; propertyValue.Raw = file; if (property.TypeInfo.IsFileStoredInDb == false && property.FileOptions.NameCreation == NameCreation.UserInput) { var providedName = (string)valueProvider.GetValue(property.Name) .ConvertTo(typeof(string), CultureInfo.CurrentCulture); propertyValue.Additional = providedName; } var isDeleted = false; if (file == null || file.ContentLength > 0) { isDeleted = false; } else { var isDeletedKey = property.Name + "_delete"; if (valueProvider.ContainsPrefix(isDeletedKey)) { isDeleted = ((bool?) valueProvider.GetValue(isDeletedKey) .ConvertTo(typeof(bool), CultureInfo.CurrentCulture)).GetValueOrDefault(); } } if (isDeleted) { propertyValue.DataBehavior = DataBehavior.Clear; propertyValue.Additional = null; } return propertyValue; }
bool IValueProvider.ContainsPrefix(string prefix) { return(_valueProvider.ContainsPrefix(prefix)); }
public bool ContainsPrefix(string prefix) { return(_innerValueProvider.ContainsPrefix(prefix)); }