well I'm doing a project where my JSON file has to give me for example this response in that format:
"activityId" : 1
"startDate" : "2023-02-12 04:00:00",
"endDate" : "2023-03-22 12:00:00",
Is there any way to get to that format using dataannotations or without using SQL? (I have no problem deleting my migration and start it again cause is a vey small project) thanks
mM class is build like this and I made the migration with EF:
[Key]
public Guid activityId { get; set; }
[DataType(DataType.DateTime)]
public DateTime startDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime endDate { get; set; }
and what I got is this:
"activityId": "3fa85f64-5717-4562-b3fc-2c963f78afa6",
"startDate": "2023-02-14T18:11:21.463Z",
"endDate": "2023-02-14T18:11:21.463Z",
Related
I have applications with a simple [HttpPost] method in controller that should accept an object from the query body (query from postman).
However, I have an error every time because null comes to the method - not object.
Model:
public class MeetupDto
{
public string Name { get; set; }
public string Organizer { get; set; }
public DateTime Date { get; set; }
public bool IsPrivate { get; set; }
}
Method:
[HttpPost]
public ActionResult Post([FromBody]MeetupDto model)
{
...
}
The method works fine when I shrink the model down to the name and organizer only. When there's a date or a bool, it doesn't work.
Postman (body raw+json):
https://localhost:44398/api/meetup/
"name": "JsEvent",
"organizer": "chrome",
"date": "2020-07-26 15:20:00",
"isPrivate": "false"
You have two problems:
You're sending a string for IsPrivate, when it's expecting a bool
You're not using the correct date format
Change your JSON to this:
// Note the 'T' in the date
{
"name": "JsEvent",
"organizer": "chrome",
"date": "2020-07-26T15:20:00",
"isPrivate": false
}
and it should bind your model correctly.
I have a entity A
public class A
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Now Description property contains JSON serialized DATA like :
{"Year":"2017","Quarter":"2"}
I'd like to know if there is a way to filter on serialized data ?
Let's say I want all A where Description has value 2017 for Year, does linq permit such request ?
Now, Description does not always contains value as JSON. Is there a way to check if Description value is JSON ?
So the final code could look like
DbContext.Set<A>().Where(a => a.Description.Contains("JSON_REGEX") && a.Description.JsonFilter(d => d.Year == 2017));
When I record such DATA I use Newtonsoft.JSON.
EF 6.1.1
I've tried several different string formats and json serializer settings but can not come up with the right combination; my date always comes out as the default min date value.
I'm doing:
_flashMessage = JsonConvert.DeserializeObject<FlashMessage>(msoSite.FlashMessage, settings);
Where FlashMessage is:
public class FlashMessage
{
public string Device { get; set; }
DateTime Expires { get; set; }
public List<string> Message { get; set; }
}
And my Json string is:
{
"Device": "Mobile",
"Expires": "2015-03-13T11:35:35",
"Message": [
"This is a test message..."
]
}
The date result I'm getting is:
The answer is: my DateTime Expires property wasn't defined as public, therefore out of scope and not getting set.
Currently I can insert and lookup entities perfectly fine, however when I do
await myTable.UpdateAsync(entity);
It correctly goes into the corresponding TableController but no entities are passed through, every field is null, or its MinValue.
I can put the same entity into an
await myTable.InsertAsync(entity);
and all variables correctly come through at the other end.
Just to add some more information, I am getting the entity originally by
Entity entity = await myTable.LookupAsync(id);
Then I update values on this entity and pass it to the UpdateAsync
The Entity definition on my mobile app is
public Guid Id { get; set; }
public String firstName { get; set; }
public String lastName { get; set; }
public DateTime added { get; set; }
[Microsoft.WindowsAzure.MobileServices.Version]
public string Version { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public bool Deleted { get; set; }
The entity definition is the same at the TableController, the only difference is that I don't specify the CreatedAt, UpdatedAt, Deleted and Version as they are part of EntityData that I am inheriting from.
At the TableController end here is some more information on the patch.
patch.GetUnchangedPropertyNames().ToList();
Count = 5
[0]: "added"
[1]: "firstName"
[2]: "lastName"
[3]: "Version"
[4]: "CreatedAt"
patch.GetChangedPropertyNames().ToList();
Count = 3
[0]: "Id"
[1]: "UpdatedAt"
[2]: "Deleted"
I also tried changing the Id to a string and removing the DateTime at both ends yet the firstName and lastName never come through.
Due to more debugging (as per my comments below) I correct my statement and say that it is at the TableController end. It is not accepting anything other than Booleans.
The dynamic view will show DateTime or Int32 but no String. For example I added an integer property to my entity and sent this through
{
"Deleted":"false",
"test":123,
"firstName":"test"
}
But here is what I get in the controller.
Just a note, I can change Deleted to true and it reflects correctly.
I have a model like the following :
public class GridViewModel
{
public List<List<KeyValuePair<string, string>>> Rows { get; set; }
public int FoundItems { get; set; }
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public int ItemsPerPage { get; set; }
public string PagingLinks { get; set; }//Contains Html Tags
}
Rows will fill dynamically in Controllers like the following :
Rows = [ [Id=1, Name='Name', FullName='FullName'], [Id=2, Name='Name', FullName='FullName'], ... ];
I wanna convert the Model to JSON for sending through JsonResult.
So, I expect something like the following in my JSON :
{
Rows : [
{ Id=1, Name='Name', FullName='FullName'},
{ Id=2, Name='Name', FullName='FullName'}
],
FoundItems : 123,
CurrentPage : 1,
TotalPages : 3,
ItemsPerPage : 50,
PagingLinks : '<b>1</b><span>2</span><span>3</span>'
}
Could you please guide me, How can I convert the model to JSON ?
A List within List made up of KeyValuePair looks wrong to me, surely you want:
List<Dict<string,string>> // Will be serialized correctly by Json.Net
Assuming you are correct in wanting List<List<KeyValuePair<string,string>>>, most of this is automatic assuming you're using the Json.Net (Newtonsoft JSON) that is included with MVC4, the structure you're going to get is fairly similar. It would default to something like:
Help page for Serializing Collections
// Assumption: you already know how to use JObject.Convert or one of the other
// serialization/deserialization classes.
{
Rows : [
{
[
{"Row1Key1":"Row1Value1"},
{"Row1Key2":"Row1Value2"}
]
},
{
[
{"Row2Key1":"Row2Value1"}
]
}
],
// etc (as in example provided)
Your mention of 'Id' has no corresponding data with that value and would be determined by position within the JSON.
If you truly want the ID part, you're going to have to supply a JsonConverter and project the collection out with your own logic (e.g. Id is off by one compared to the index).