My WebApi action returns a dynamic object built from JObject.parse(jsonString);
I have GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
But that object is wrapped inside a default result message in the response.
According to this article returning anonymous objects is fine and should give the expected result
I am using an async controller because I have to await some ReadAsStringAsync() here the protoype of my action:
public async Task<dynamic> Pics(string flavor, int pagesize)
Expected result :
{"flavor":"","maxFeedSize":0,"mediaContent":[]}
Result I have when returning the dynamic object:
{
"Result": {
"flavor": "",
"maxFeedSize": 0,
"mediaContent": []
},
"Id": 1,
"Exception": null,
"Status": 5,
"IsCanceled": false,
"IsCompleted": true,
"CreationOptions": 0,
"AsyncState": null,
"IsFaulted": false
}
As I thought and as mentioned in comments. I was returning a Task<Task<dynamic>> because of a naive method overload.
public async Task<dynamic> Pics(string flavor, string pagesize)
{
return Pics(flavor, pagesize, null);
}
Edit:
I tried this because unlike MVC routes ommit a string parameter throws an error even if string is nullable
public async Task<dynamic> Pics(string flavor, string pagesize, string startid =null)
works fine :)
Related
I have very simple code (asp .net mvc c#) but just couldn't make it work. The companyId is always zero, while the booleans are returning correctly. My postdata is class called SomeObj with the ff properties
public bool isSomeBoolean1 { get; set; }
public bool isSomeBoolean2 { get; set; }
public bool isSomeBoolean3{ get; set; }
public int CompanyId { get; set; }
js:
var formData = formModule.createFormData(containerSelector + someContainer, false);
formData.append("companyId", $("#myCompanyId").val());
var promise = baseAjaxWithUpload('/SomeController/SomeAction', 'POST', formData).execute();
promise.done(function (operationStatus) {
if (operationStatus.isSuccess) {
}
});
The boolean properties returns correctly according to my input in form, but not CompanyId. I have tried wrapping it in JSON.stringify, made the companyId string type, put it back to int but parseInt(CompanyId) before passing. I also made the "CompanyId" to "companyId" but nothing worked.
I made sure that the formdata has value coz I typed in console formData.get("CompanyId") or formData.get("companyId") when I changed it to that spelling, both have values, but turns to zero in the controller.
I have also tried doing this:
var data = {
CompanyId: $("#myCompanyId").val(),
isSomeBoolean1 : true,
isSomeBoolean2 : true,
isSomeBoolean2: false,
}
var promise = baseAjaxWithUpload('/SomeController/SomeAction', 'POST', JSON.stringify(data) ).execute();
all booleans are passed correctly in controller just like formData, but not companyid. it always is 0;
this is my controller that uses it.
[HttpPost]
public ActionResult SomAction(SomeObj someObj )
{
}
I have also tried isolating the issue by adding a property called CompanyIdString with type string, then put hardcoded values like this formData.append("companyIdString", "test"), and tried to peek the value thru get, and it has, but that string returns null in controller. I also tried upper case spelling.
I mean I have been passing companyId all over the app, and never had a problem until now. What am I missing?
You can try to use application/x-www-form-urlencoded content type in the AJAX call:
var data = {
CompanyId: $("#myCompanyId").val(),
isSomeBoolean1 : true,
isSomeBoolean2 : true,
isSomeBoolean2: false,
}
$.ajax({
url: 'myUrl',
type: "POST",
contentType: 'application/x-www-form-urlencoded',
data: data,
success: function (result) {
console.log(result);
}
});
I am trying to implement sorting on a custom grid I am working on and having an issue with jQuery to MVC parameter binding.
I have a Jquery request like the one shown below
// Javascript
var dataobj =
{
test: 3,
sortInfo: self.sortInfo,
pagingInfo: {
TotalItems: 34, //headercontainer.attr("data-pagingInfo-TotalItems"),
ItemsPerPage: headercontainer.attr("data-pagingInfo-ItemsPerPage"),
CurrentPage: headercontainer.attr("data-pagingInfo-CurrentPage")
}
};
$.ajax({
url: self.viewModel.GenericGridHeaderModel.SortingCallbackUrl,
type: 'POST',
data: dataobj,
dataType: "json",
success: function (html) {...}
});
// C#
public PartialViewResult GenericGridSort(int test, SortInfo sortInfo, PagingInfo pagingInfo){
...
}
At the moment I have non null values in sortInfo object in Javascript and I see that the values are posted correctly however inside the action method the values are not getting bound correctly. All I see is default values for the sortInfo and pagingInfo parameters. In fact the test parameter is getting the value 3 correctly.
For clarity here is my sortInfo model
public enum SortDirection
{
None = 0,
Ascending = 1,
Descending = 2
}
public class SortInfo
{
public int FieldIndex { get; set; }
public string FeildName { get; set; }
public SortDirection SortDirection { get; set; }
}
Can anyone tell me what am I missing here ?
Thanks all !
I believe that you are not encoding the JSON payload.
You should be using either:
data: $.toJSON(dataobj),
or
data: JSON.stringify(dataobj),
Also, for contentType use:
contentType: 'application/json; charset=utf-8',
Here is more information on POSTing JSON payload to MVC
Also, in the dataType option you specify the type of the return value, in your case, it looks like the action method will be returning HTML, but you are specifying JSON.
Wow, as soon as I know that will not work. When you attempt to pass inner objects in your ajax data you must reference the inner objects properties strictly in the root of data ("innerobject.property": value)
Example:
var dataobj =
{
test: 3,
"sortInfo.property1": self.sortInfo.property1,
/* other properties */
"pagingInfo.TotalItems": 34,
//headercontainer.attr("data-pagingInfo-TotalItems"),
"pagingInfo.ItemsPerPage": headercontainer.attr("data-pagingInfo-ItemsPerPage"),
"pagingInfo.CurrentPage": headercontainer.attr("data-pagingInfo-CurrentPage")
};
I've been playing around with asp.net web api, and I noticed that default generated returned json doesn't include the object level key. I was also wondering how to customize the output of the json string. For example:
Getting a User usually returns
{
"Name": "ChaoticLoki",
"Age": 22,
"Sex": "Male"
}
I was hoping I could return something like:
{
"data": {
"Name": "ChaoticLoki",
"Age": 22,
"Sex": "Male",
},
"success": true
}
You can then create a class wrapping the data and status like this
public class JsonResult{
public object data { get; set;}
public boolean success { get; set;}
}
And then in your Web Api methods, you can do
var data = ... //getting from repo
return Json(new JsonResult{data = data, success = true});
I am using Breeze against a standard RPC style WebAPI. I have decorated the controller with the [BreezeController] attribute. I have defined entity metadata on the client for the entities being returned by the WebAPI actions. My Breeze DataService is setup as follows:
var dataService = new breeze.DataService({
serviceName: "http://localhost/api/PartReceiptPurchaseOrders",
hasServerMetadata: false
});
When calling the EntityManager's SaveChanges method after modifying an entity, the EntityInfo object on the server is empty. It appears the serialized entity passed to the SaveChanges method is not being deserialized properly into the expected entity on the server. I'm having a hard time understanding what I'm doing wrong.
When I inspect the JObject saveBundle argument passed to the SaveChanges method on the WebAPI controller, I get the expected entity details:
{
"entities": [{
"PurchaseOrderPartId": 1,
"PartNumber": "ABC",
"SupplierPartNumber": "12345",
"Description": "Some Part",
"Bin": "1",
"Qty": 24,
"QtyReceived": 24,
"QtyBackordered": 0,
"Cost": 60,
"Currency": "USD",
"PurchaseOrderId":1,
"entityAspect": {
"entityTypeName": "PurchaseOrderPart:#MyApp.Models",
"entityState": "Modified",
"originalValuesMap": {
"QtyReceived": 0
},
"autoGeneratedKey":{
"propertyName": "PurchaseOrderPartId",
"autoGeneratedKeyType": "Identity"
}
}
}],
"saveOptions": { "allowConcurrentSaves": false }
}
However, after the call to the base class method Breeze.WebApi.ContextProvider.SaveChanges() the entityInfo.Entity property contains an empty object as follows:
entityInfo {Breeze.WebApi.EntityInfo}
AutoGeneratedKey: null {Breeze.WebApi.AutoGeneratedKey}
Entity {MyApp.Models.PurchaseOrderPart}
Bin: null
Cost: 0
Currency: null
Description: null
PartNumber: null
PurchaseOrder: null {MyApp.Models.PurchaseOrder}
PurchaseOrderId: 0
PurchaseOrderPartId: 0
Qty: 0
QtyBackordered: 0
QtyReceived: 0
SupplierPartNumber: null
If I breakpoint into the CreateEntityInfoFromJson in the Breeze.WebApi.ContextProvider class, I see that the call to jsonSerializer.Deserialize(new JTokenReader(jo), entityType) sets entityInfo.Entity to an empty entity object. There is no error raised during the deserialization so I can't tell why this is happening.
Can anyone point me towards a possible resolution?
Thanks,
Richard
Ok, I figured this out and it was a dumb mistake on my part. My entity type on the server had been declared with internal setters like public decimal QtyReceived { get; **internal** set; }. This meant the JSON deserializer couldn't set the property value. Interestingly enough, the error is just ignored by the deserilizer.
Changing the setters to be public fixed this issue.
I want to create like this:
[
['date', 'T1', 'T2', 'T3'],
['2000', 1, 1, 0.5],
['2001', 2, 0.5, 1],
['2002', 4, 1, 0.5],
['2003', 8, 0.5, 1],
['2004', 7, 1, 0.5]
]
from my model
public partial class AylikOkumaIstatistik
{
public string sno { get; set; }
public string date{ get; set; }
public Nullable<decimal> TotalUsageValue { get; set; }
public string UsageType { get; set; } // T1,T2,T3
}
This is possible with using json? If it is possible, is there any code example or tutorial about this topic.How can I convert data that is from database to json?
Thanks.
This is possible with using json?
Of course, you could use the object[] type as view model (where each element is itself an object[]):
public ActionResult Index()
{
object[] model = new[]
{
new object[] { "date", "T1", "T2", "T3" },
new object[] { "2000", 1, 1, 0.5 },
new object[] { "2001", 2, 0.5, 1 },
new object[] { "2002", 4, 1, 0.5 },
new object[] { "2003", 8, 0.5, 1 },
new object[] { "2004", 7, 1, 0.5 },
};
return Json(model, JsonRequestBehavior.AllowGet);
}
As far as converting the data that is in your database to this view model is concerned, well, you will have to give an example of how is your data represented in the database. Ideally provide a hardcoded example of how your model will look like. But it will be a simple matter of projecting your domain model to an object[].
If you can use Mvc 4? you can add WebApi controller to manage json requests. You will need just to return your entity in WepApi controller method and it will be automatically converted into json object and vice versa. It will work even if you have json objects in post requests body, they will also map to you .net entities and come into controller method as incoming params. If you need i can give any examples you need.