Passing JSON to WEB API Controller - asp.net-mvc

I have this model for an MVC WEB API controller.
What will be the corresponding JSON to match this model structure?
namespace CarEvaluator.Models
{
[DataContract]
public class Record
{
[DataMember]
public List<Boolean> CHits { get; set; }
[DataMember]
public List<Boolean> MHits { get; set; }
}
}
public void Post(Record record)
{
}

Structure:
{"CHits":[true,false],"MHits":[true,false]}
Example:
var postObject = new Object();
// Initialize CHits and MHits as arrays
postObject.CHits = [];
postObject.MHits = [];
// push some items into the arrays
postObject.CHits.push(true);
postObject.CHits.push(false);
postObject.MHits.push(true);
postObject.MHits.push(false);
// serialize data to post
// this is what you set as data property in for instance jquery ajax
$.ajax({
//other params, content type etc
type: 'POST',
data: JSON.stringify(postObject),
...
});
if your parameter is null, you should try to add the [FromBody] attribute and decorate the method with httppost
[HttpPost]
public void Post([FromBody]Record record)
{
}

Related

How to order by last modified in RavenDB?

I'm trying to allow my entities to be ordered by the Last-Modified property in their metadata, using OData query options.
I tried using a transformer as described in Converting to JSON and accessing metadata, but when I apply ODataQueryOptions to the resulting IQueryable, I get an empty array.
The model and view-model:
public class Foo
{
public int Id { get; set; }
}
public class FooViewModel
{
public string Id { get; set; }
public DateTime LastModified { get; set; }
}
The transformer:
public class Foos_WithLastModified : AbstractTransformerCreationTask<Foo>
{
public Foos_WithLastModified()
{
TransformResults = foos => from foo in foos
let metadata = MetadataFor(foo)
select new
{
Id = foo.Id.ToString(CultureInfo.InvariantCulture),
LastModified = metadata.Value<DateTime>("Last-Modified")
};
}
}
The relevant method in FooController (_session is an IAsyncDocumentSession):
public async Task<ICollection<FooViewModel>> Get(ODataQueryOptions<FooViewModel> options)
{
var settings = new ODataValidationSettings();
settings.AllowedOrderByProperties.Add("LastModified");
options.Validate(settings);
var foos = _session.Query<Foo>()
.TransformWith<Foos_WithLastModified, FooViewModel>();
var odataFoos = (IQueryable<FooViewModel>)options.ApplyTo(foos);
return await odataFoos.ToListAsync();
}
When I hit /api/Foo, the results are as expected:
[
{
"Id": "foos/456",
"LastModified": "2015-11-23T08:43:10.913662Z"
},
{
"Id": "foos/123",
"LastModified": "2015-11-23T08:50:34.0907996Z"
}
]
But when I add OData query options (/api/Foo?$orderby=LastModified), I get an empty array: [].
I also tried changing _session to an IDocumentSession and modifying Get as follows,
[EnableQuery(AllowedOrderByProperties = "LastModified")]
public IQueryable<FooViewModel> Get()
{
return _session.Query<Foo>()
.TransformWith<Foos_WithLastModified, FooViewModel>();
}
but I get the same results.
Are transformers the wrong approach? How can I sort by Last-Modified using OData query options?
I do not know how to handle the OData stuff, never tried that, but in order to query for entities, ordered by the metadata value "Last-Modified" using only RavenDB techniques you can do the following:
Create an index for your entity (in my example a Customer). In this index we add the field LastModified that's using the document's metadata value for Last-Modified.
public class Customer_ByLastModified : AbstractIndexCreationTask<Customer>
{
public class QueryModel
{
public DateTime LastModified { get; set; }
}
public Customer_ByLastModified()
{
Map = customers => from customer in customers
select new
{
LastModified = this.MetadataFor(customer).Value<DateTime>("Last-Modified")
};
}
}
The QueryModel isn't mandatory, but it makes querying via the client API easier, imo. You can then add a Transformer to be able to use the metadata value in your return model:
public class Customers_WithLastModified : AbstractTransformerCreationTask<Customer>
{
public Customers_WithLastModified()
{
TransformResults = results => from customer in results
select new CustomerViewModel
{
Id = customer.Id,
Name = customer.Name,
LastModified = MetadataFor(customer).Value<DateTime>("Last-Modified")
};
}
}
And then query it like this:
using (var session = documentStore.OpenSession())
{
var customers = session.Query<Customer_ByLastModified.QueryModel, Customer_ByLastModified>()
.OrderByDescending(x => x.LastModified)
.TransformWith<Customers_WithLastModified, CustomerViewModel>()
.ToList();
}
Hope this helps!

How to return a property with IHttpActionResult in Web API

I have a model class like so (generated from EF):
public partial class Point
{
public int Id { get; set; }
public int First { get; set; }
public int Second { get; set; }
public int Total { get; set; }
}
and a Post method (which is called from Angular) in my controller (ApiController) like so:
[ResponseType(typeof(Point))]
public IHttpActionResult PostPoint(Point points)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
points.Total = points.First + points.Second;
db.ScoreBoard.Add(points);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = points.Id }, points);
}
This will return the whole points object and my POST call will have a Response with all the properties of that object.
Now what I want to do is to return only the Total property and so I tried to changed the return of the PostPoint to :
return CreatedAtRoute("DefaultApi", new { id = points.Id }, points.Total);
Doing this however will return an empty Response to the POST call from Angular.
The Angular post-method looks like this:
this.post = function (Point) {
var request = $http({
method: "post",
url: "/api/PointsAPI",
data: JSON.stringify(Point)
});
return request;
}
How would I go about only returning the Total property of the pointsobject?
It will be empty because you are returning a primitive data type. Your Total property is just a number so when angular parses the response it expects an object and all it sees is "2" (if your Total was 2). So, wrap it in an object and angular will be able to parse it.
return CreatedAtRoute("DefaultApi", new { id = points.Id }, new { points.Total });
This will return { "Total" : 2 } in your body. Now angular will have an object with a Total property.

Null Reference Error when directly assigning a List object to an PagedList

KeyMaster Model
public class KeyMasterModel
{
public int KeyId { get; set; }
public int TypeId { get; set; }
public string TypeName { get; set;}
}
PagedKeyModel
public class PagedKeyModel
{
// Collection of our KeyMasterModel , trying to populate this and send it to ViewModel to display a Grid//
public IPagedList<Security.Models.KeyMasterModel> pagedkeymaster;
}
//ViewModel//
public class KeyMasterViewModel
{
//tell me how to initialise the KeyMasterViewModel.pagedkeymodel.pagedkeymaster in a constructor here so that I dont get a null
public PagedKeyModel pagedkeymodel;
public KeyMasterModel keymastermodel;
}
//Controller//
[HttpGet]
public ActionResult ListOfKey(string sortOrder, string CurrentSort, int? page)
{
// View Model Object //
KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();
// At the gollowing step keyMasterModelObject has values retrieved from db //
//Next aim is to place it into ViewModel Object by assigning the retrieved PagedList of KeyMasterModel to IPagedList<Security.Models.KeyMasterModel> pagedkeymaster
IPagedList<KeyMasterModel> KeyMasterModelObject= datalayercall.GetAll(sortOrder, CurrentSort, page);
// Here is where error is thrown , all of a sudden I get the error , KeyMasterModelObject becomes null .
// I am trying finally to populate everything into ViewModel object
keyMasterViewModel .pagedkeymodel.pagedkeymaster = KeyMasterModelObject;
return View(keyMasterViewModel);
}
// Business Logic Layer
public IPagedList<KeyMasterModel> GetAll(string sortOrder, string CurrentSort, int? page)
{
var retrieveddatalayerobject = datalayerobject.KeyMasters;
// Retrieving Data from db and forming a list according to my model in App//
List<KeyMasterModel> keymastermodellist = new List<KeyMasterModel>();
KeyMasterModel keymastermodelobject=new KeyMasterModel();
foreach(var retrieveditems in retrieveddatalayerobject)
{
keymastermodelobject.KeyId = retrieveditems.KeyId;
keymastermodelobject.TypeName = retrieveditems.TypeMaster.TypeName;
// Create a New List Of KeyMasterModel //
keymastermodellist.Add(keymastermodelobject);
}
// Paged List of KeyMaster Model //
IPagedList<KeyMasterModel> IPagedListKeyMasterModel = null;
switch (sortOrder)
{
case "KeyId":
if (sortOrder.Equals(CurrentSort))
IPagedListKeyMasterModel = keymastermodellist.OrderByDescending
(m => m.KeyId).ToPagedList(pageIndex, pageSize);
}
return IPagedListKeyMasterModel;
}
Your code as I understand it:
KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();
[...]
keyMasterViewModel.pagedkeymodel.pagedkeymaster = KeyMasterModelObject;
So here you are using keyMasterViewModel.pagedkeymodel and assuming it is not null (because you are trying to assign to a property on it). However KeyMasterViewModel doesn't have a constructor and doesn't set pagedkeymodel to anything at declaration so it will default to being null. Hence your problem.

How to pass a ko.observablearray via JSON to an MVC controller

I'm using Knockout JS to build a model to pass to an MVC controller. The ko.observable() items are passed to the controller no problem, however, the ko.observableArray([]) data is appearing as "count=0" at the controller.
Below is the object I am building in my View:
var AddViewModel = function () {
self.ModelRequest = {
Object: {
VarArray: ko.observableArray([]),
Var1: ko.observable(""),
Var2: ko.observable("")
}
};
....
The ModelRequest.Object.VarArray is an ko.observableArray contains a few attributes in the object: Name, Id, Code, Type.
Below is how I'm sending the data via JSON:
p = ko.toJSON(AddViewModel.ModelRequest);
debugger;
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(AddViewModel.ModelRequest),
success: function (data) {
...something...
}
});
When I am debugging the code, I examine the p variable described above and I see the below:
{"Object":{"VarArray":[{"Name":"Name 1", "Id":2, "Code":"50.1", "Type":"A"}],
"Var1":"abc", "Var2":"def"}}
When I examine the object being passed into the controller, Var1 and Var2 have the correct values, however, the VarArray is "Count=0".
Any thoughts? Thanks for taking the time to look at this. I'll try any ideas at this point!!
EDIT 10/6/13:
This is my controller action:
[HttpPost]
public CRUDResponse AddItem(AddRequest ModelRequest)
{
... something here ...
}
At this point when I examine the ModelRequest I see that VarArray is "Count = 0".
Edit 10/8/13:
This is the details of the AddRequest:
#region Public Members
public ObjectType Object { get; set; }
#endregion Public Members
Where the ObjectType is:
#region Public Members
public int Var1 { get; set; }
public int Var2 { get; set; }
public List<SpecType> VarArray { get; set; }
#endregion Public Members
Where the SpecType is
public string Name { get; set; }
public int Id { get; set; }
public string Code { get; set; }
public FieldType Type { get; protected set; }
And the FieldType is a Enum.
UPDATE: I had just found the problem. It looks like the property is not getting serialized properly through JSON when I make a call to my Web API from the UI. The above-mentioned property is of type TypaA which inherits from TypeB. TypeB contains all of the fields needed by TypeA. When I change the property failing to serialize to be of type TypeB, instead of TypeA, it serializes just fine and I get all of the values I need reflected in Web API.
So, basically, JSON fails to serialize a value if it's type is derived from another type. Removing the inheritance by declaring a value to be of base type fixes the issue.
So, is there a way to serialize a property whose type inherits from another class?
Eric
I think the problem is that either A: you are never populating the observableArray, or B: you are not receiving the proper object type back on the controller, either because you are sending it incorrectly or receiving it improperly.
Try doing this -
function testData(name) {
var self = this;
self.Name = ko.observable(name);
}
inside of your view model
var AddViewModel = function () {
self.ModelRequest = {
Object: {
varArray: ko.observableArray([
new testData('Your my boy blue'),
new testData('Frank the tank')
]),
var1: ko.observable(""),
var2: ko.observable("")
}
};
}
And see if your controller action is actually getting your data back.
If not then you are most likely not matching the object you are sending to the controller with an object the controller recognizes.

Knockout and MVC3: Posting JSON to action, serializing twice? Can't convert to C# objects?

I've got a Knockout Model that gets posted via a save method:
self.save = function(form) {
ko.utils.postJson($("form")[0], self);
};
I check the request to make sure all the data is properly being posted (it is):
However, when I get to my action:
[HttpPost]
public ActionResult Create(EquipmentCreateModel equipmentCreateModel)
{
/stuff here
}
BuildingCode and Room contain escaped quotes, and identifiers is totally not null but has a count of 0:
And my ModelState is not valid, there is one error, for the Identifiers property which has an attempted value of :
and the Exception message is:
"The parameter conversion from type 'System.String' to type 'System.Collections.Generic.KeyValuePair`2[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' failed because no type converter can convert between these types."
My Model:
public class EquipmentCreateModel
{
//used to populate form drop downs
public ICollection<Building> Buildings { get; set; }
public ICollection<IdentifierType> IdentifierTypes { get; set; }
[Required]
[Display(Name = "Building")]
public string BuildingCode { get; set; }
[Required]
public string Room { get; set; }
[Required]
[Range(1, 100, ErrorMessage = "You must add at least one identifier.")]
public int IdentifiersCount { get; set; } //used as a hidden field to validate the list
public string IdentifierValue { get; set; } //used only for knockout viewmodel binding
public IDictionary<Guid, string> Identifiers { get; set; }
}
Now first I thought it was a problem with knockout, but then I found out the data wasn't being posted in the request correctly. I fixed that and still had the same problem. I thought MVC3 automatically converts Json now? Why are my simple properties appearing in escaped quotes and why can't my identities collection properly populate from the posted data?
Try this:
[HttpPost]
public ActionResult Create([FromJson] EquipmentCreateModel equipmentCreateModel)
{
//stuff here
}
where FromJson is:
public class FromJsonAttribute : CustomModelBinderAttribute
{
private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();
public override IModelBinder GetBinder()
{
return new JsonModelBinder();
}
private class JsonModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
if (string.IsNullOrEmpty(stringified))
return null;
return serializer.Deserialize(stringified, bindingContext.ModelType);
}
}
}
This is taken from:
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
you should check the comments to as there are some modification for the FromJsonAttribute.
If you are using MVC3 you don't need to add JsonValueProviderFactory. For those of us who are still on MVC2 you can add JsonValueProviderFactory manually
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
However JsonValueProvider only works with AJAX post. For the full postback it needs the extra processing. There's a thread describing how to handle full postback: groups.google.com/d/topic/knockoutjs/3FEpocpApA4/discussion
The easiest solution would be to use AJAX post. Change
ko.utils.postJson($("form")[0], self);
to
$.ajax({
url: $("form").action,
type: 'post',
data: ko.toJSON(self),
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
You could try:
[HttpPost]
public ActionResult Create(string equipmentCreateModelString)
{
var equipmentCreateModel = JsonConvert.DeserializeObject<EquipmentCreateModel> equipmentCreateModelString, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
}
Otherwise you need to use a JsonValueProviderFactory. Here's an example
#DOTang, i have another approach. First, you need a clean js object from your view model. You can get it calling: ko.mapping.toJS(self), then pass your view model to postJson function as a property. Finally add [FromJson] attribute to your controller. Your controller argument name, must be equal to your js property name, in this case: model.
I hope it works for you as works for me.
server
[HttpPost]
public ActionResult RegisterUser([FromJson] EquipmentCreateModel model)
{
//...
}
client
self.save = function() {
var jsModel = ko.mapping.toJS(self);
ko.utils.postJson('#Url.Action("Create", "Equipment")', { model : jsModel });
}

Resources