我发送发布请求并获得响应正文表单数据:0[family]=Marco&0[name]=Polo&0[age]=66&1[family]=Family&0[name]=Name&0[age]=22
var formData = "0[family]=Marco&0[name]=Polo&0[age]=66&1[family]=Family&0[name]=Name&0[age]=22";
var data = new Dictionary<string, string>();
var preData = HttpUtility.ParseQueryString(formData);
foreach (string key in preData.AllKeys)
{
if (key != null)
{
data.Add(Replace(key), preData[key]);
}
}
我有模型:
public class User {
public string Family {get;set;}
public int Age {get;set;}
}
对于绑定我使用DefaultModelBinder
:
private static bool TryUpdateModel(User user, IDictionary<string, string> formdata, ModelStateDictionary modelState) where TModel : class
{
var binder = new DefaultModelBinder();
var valueProvider = new DictionaryValueProvider<string>(formdata, CultureInfo.InvariantCulture);
var bindingContext = new ModelBindingContext
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => user, typeof(User)),
ModelState = modelState,
PropertyFilter = propertyName => true,
ValueProvider = valueProvider
};
var ctx = new ControllerContext();
binder.BindModel(ctx, bindingContext);
return modelState.IsValid;
}
- 我想知道我有响应“0 [name] = Polo”的数据,它没有绑定到模型用户。模型用户中不存在属性“名称”。
- 我想知道是否有更多数据作为响应,然后我们可以绑定。
我该怎么做?