Binding DataSourceLoadOptions in ASP.NET Boilerplate dynamic controllers
See original GitHub issueStarted in https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/231#issuecomment-396456741
If I understand correctly, you are talking about ASP.NET Boilerplate dynamic controllers. According to their docs, model binders should work (see our docs and example). Replacing fields with properties won’t make binding automatic.
When model binders don’t work, you can use DataSourceLoadOptionsParser
directly from an action method:
var loadOptions = new MyLoadOptions(); // a subclass of DataSourceLoadOptions
var query = Request.GetQueryNameValuePairs();
DataSourceLoadOptionsParser.Parse(loadOptions, name => query.FirstOrDefault(i => i.Key == name).Value);
or
var loadOptions = new MyLoadOptions(); // a subclass of DataSourceLoadOptions
DataSourceLoadOptionsParser.Parse(loadOptions, name => Request.QueryString[name]);
@AlekseyMartynov correct. If I make a subclass with getters/setters and then return back a DataSourceLoadOption it works… My original issue is the dynamic controller created an empty parameter list for the the DataSourceLoadOption class.
Not sure if youre familiar with the framework but the dynamic api converts app services over to webapi and js actions.
EX: You see how the 2nd method has no input variables. (If I make a subclass with getters/setters etc, it generates the parameters)
[DontWrapResult]
public virtual object GetAllDevExtremeByJsonOptions(string input, bool withIncludes = true)
{
CheckGetPermission();
DataSourceLoadOptions options;
if (!string.IsNullOrEmpty(input))
{
options = JsonConvert.DeserializeObject<DataSourceLoadOptions>(input, new JsonSerializerSettings {ContractResolver = new DefaultContractResolver()});
}
else
{
throw new UserFriendlyException("LoadOptions is Null!", "The load options should not be empty for a DevExtreme end point!");
}
return GetAllDevExtreme(options, withIncludes);
}
[DontWrapResult]
public virtual object GetAllDevExtreme(DataSourceLoadOptions input, bool withIncludes = true)
{
CheckGetPermission();
return DataSourceLoader.Load(GetAllQueryable(withIncludes), input).ToJsonString(false, true);
}
// action 'getAllDevExtremeByJsonOptions'
abp.services.app.bRC.getAllDevExtremeByJsonOptions = function(input, withIncludes, ajaxParams) {
return abp.ajax($.extend(true, {
url: abp.appPath + 'api/services/app/BRC/GetAllDevExtremeByJsonOptions' + abp.utils.buildQueryString([{ name: 'input', value: input }, { name: 'withIncludes', value: withIncludes }]) + '',
type: 'GET'
}, ajaxParams));;
};
// action 'getAllDevExtreme'
abp.services.app.bRC.getAllDevExtreme = function(input, withIncludes, ajaxParams) {
return abp.ajax($.extend(true, {
url: abp.appPath + 'api/services/app/BRC/GetAllDevExtreme' + abp.utils.buildQueryString([{ name: 'withIncludes', value: withIncludes }]) + '',
type: 'GET'
}, ajaxParams));;
};
This is what I was trying to do: (which now works if I just pass the JSON over and convert in the method above, Is there a better way that you know of?)
var gridData = new DevExpress.data.DataSource({
load: function(loadOptions) {
abp.log.debug("Initial Load Options:");
abp.log.debug(loadOptions);
abp.log.debug(stringToPascalJSON(loadOptions));
var deferred = $.Deferred();
abp.services.app.bRC.getAllDevExtremeByJsonOptions(JSON.stringify(stringToPascalJSON(loadOptions)))
.done(function(result) {
abp.log.debug("Response");
abp.log.debug(result);
deferred.resolve({ data: result.data, totalCount: result.totalCount });
});
return deferred.promise();
},
byKey: function(key) {
abp.log.debug("ByKey: " + key);
return { id: key };
}
});
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
It’s great to hear that you have found a good solution. Another good case for using
RemoteController
!To make it even better, I can suggest to use
Url.Content
to deal with relative URLs:This can be closed, I figured out my issues finally. Part of them were related to the fact that the webapi is dynamic and that its webapi and only allows for 1 parameter. So I made a method that took a jobject and parsed the data out of that rather than passing in 1 object to body and rest via querystring.