Iincomplete validation - asp.net-core

There is a model:
public class Model
{
[Required(AllowEmptyStrings = false)]
public string Code { get; set; }
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
[MaxLength(6), MinLength(6)]
public string Color { get; set; }
[MaxLength(3)]
public string Text { get; set; }
[Required]
public int? User { get; set; }
}
If I pass null data in the User, Color, and Text fields, then the answer is:
{
"code": "VALIDATION",
"fields": [
{
"field": "user",
"error": "REQUIRED"
}
]
}
Why I do not get a complete list of required fields?

Related

How to define the name of the data for the ajax.datatable column name? I keep getting the unknow parameter error for ajax.datatable

JobProg.js
var dataTable;
$(document).ready(function () {
loadDataTable();
});
function loadDataTable() {
dataTable = $('#tblData').DataTable({
"ajax": {
"url": "/Admin/CMJobProg/GetAll"
},
"columns": [
{ "data": "jobnumber", "width": "35%" },
{ "data": "assignfrom", "width": "35%" },
{ "data": "assignto", "width": "35%" },
{ "data": "dateassign", "width": "35%" },
{ "data": "jobaction", "width": "35%" },
{ "data": "remarks", "width": "35%" },
{ "data": "type", "width": "35%" },
{ "data": "division" ,"width": "35%" }
]
})
}
CMJobProg.cs
namespace LXG.Models
{
[Table("CMJOBPROG", Schema = "LASIS")]
public class CMJobProg
{
[Required]
[MaxLength(8)]
[Display(Name = "Job Number")]
[Column("JOB_NUMBER")]
public string JobNumber { get; set; }
[Display(Name = "Assign From")]
[MaxLength(22)]
[Column("ASSIGN_FROM")]
public string AssignFrom { get; set; }
[Display(Name = "Assign To")]
[MaxLength(22)]
[Column("ASSIGN_TO")]
public string AssignTo { get; set; }
[Column("DATE_ASSIGN")]
public DateTime DateAssign { get; set; }
[Display(Name = "Job Action")]
[Range(0, 999)]
[Column("JOB_ACTION")]
public int? JobAction { get; set; }
[Display(Name = "Remarks")]
[MaxLength(500)]
[Column("REMARKS")]
public string Remarks { get; set; }
[Display(Name = "Job Type")]
[Required]
[MaxLength(2)]
[Column("TYPE")]
public string Type { get; set; }
[MaxLength(2)]
[Column("DIVISION")]
public string Division { get; set; }
}
}
DataTables warning: table id=tblData - Requested unknown parameter 'jobnumber' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
I keep getting this error, how to fix this? I able to get the data remarks, type and division, but i cant get others data. Acutally how to define the name for the data name?
As far as I know, asp.net core returned json will has a special format. Like this : JobNumber to jobNumber. To solve this issue. I suggest you could set a JsonPropertyName attribute for each model property.
More details, you could refer to below codes:
[Table("CMJOBPROG", Schema = "LASIS")]
public class CMJobProgress
{
[Required]
[MaxLength(8)]
[Display(Name = "Job Number")]
[Column("JOB_NUMBER")]
[JsonPropertyName("jobnumber")]
public string JobNumber { get; set; }
[Display(Name = "Assign From")]
[MaxLength(22)]
[Column("ASSIGN_FROM")]
[JsonPropertyName("assignfrom")]
public string AssignFrom { get; set; }
[Display(Name = "Assign To")]
[MaxLength(22)]
[Column("ASSIGN_TO")]
[JsonPropertyName("assignto")]
public string AssignTo { get; set; }
[Column("DATE_ASSIGN")]
[JsonPropertyName("dateassign")]
public DateTime DateAssign { get; set; }
[Display(Name = "Job Action")]
[Range(0, 999)]
[Column("JOB_ACTION")]
[JsonPropertyName("jobaction")]
public string JobAction { get; set; }
[Display(Name = "Remarks")]
[MaxLength(500)]
[Column("REMARKS")]
[JsonPropertyName("remarks")]
public string Remarks { get; set; }
[Display(Name = "Job Type")]
[Required]
[MaxLength(2)]
[Column("TYPE")]
[JsonPropertyName("type")]
public string Type { get; set; }
[MaxLength(2)]
[Column("DIVISION")]
[JsonPropertyName("division")]
public string Division { get; set; }
}
Result:
Follow case-sensitive column name in your model/entity class. For example,
"data": "jobnumber"
to
"data": "JobNumber"
Add this "AddNewtonsoftJson" in Startup.cs
services.AddControllersWithViews().AddRazorRuntimeCompilation().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

Empty list in Ravendb index creating exception

I have a problem that an index I've made when queried is throwing an exception :
Could not read value for property: Members
Here is the index :
public class GroupsNameIdIndex : AbstractIndexCreationTask<CommunityDocument, GroupsNameIdIndex.Result>
{
public class Result
{
public string CommunityId { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public IList<string> Members { get; set; }
}
public GroupsNameIdIndex()
{
Map = communities => from community in communities
from newGroup in community.Groups
select new
{
CommunityId = community.Id.Replace("CommunityDocuments/", string.Empty),
newGroup.Id,
newGroup.Name,
newGroup.Members
};
StoreAllFields(FieldStorage.Yes);
}
}
This is the query :
var groupResult = session
.Query<GroupsNameIdIndex.Result, GroupsNameIdIndex>()
.Where(x => x.CommunityId == info.Id)
.AsProjection<GroupsNameIdIndex.Result>()
.ToList();
I only have 1 group in the document with relevant Id and the Members node is an empty list, not null. When I manually populate it with a single string, then the query runs fine. Why are empty lists not allowed? If this is a restriction, it makes indexing pointless for me, because I will have a lot of empty lists that are just going to make the application fail everywhere.
edit: Adding the class in case that has something to do with it:
public class CommunityGroup
{
public CommunityGroup()
{
Members = new List<string>();
MemberDetails = new List<MemberView>();
MemberNames = new List<string>();
}
public string Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public bool AdminOnly { get; set; }
public bool NewsContribute { get; set; }
public bool AutoGroup { get; set; }
public int LowerAgeLimit { get; set; }
public int UpperAgeLimit { get; set; }
public string Gender { get; set; }
public IList<string> Members { get; set; }
public IList<string> MemberNames { get; set; }
public IList<MemberView> MemberDetails { get; set; }
public string RelatedCommunityId { get; set; }
public string CreatedBy { get; set; }
public bool SmartGroup { get; set; }
public IList<SmartGroupRules> AndRules { get; set; }
public IList<SmartGroupRules> OrRules { get; set; }
public string ParentGroup { get; set; }
public List<string> EntryGroups { get; set; }
public bool Hidden { get; set; }
public string ActivityType { get; set; }
public List<string> AnyGroupList { get; set; }
public List<string> AllGroupList { get; set; }
public GroupType GroupType { get; set; }
}
and here is the json of the group in the db
"Groups": [
{
"Id": "5ja34tefoq7sfj",
"Name": "new test",
"Slug": "new-test",
"Description": null,
"AdminOnly": true,
"NewsContribute": false,
"AutoGroup": false,
"LowerAgeLimit": 0,
"UpperAgeLimit": 0,
"Gender": null,
"Members": [],
"MemberNames": [],
"MemberDetails": [],
"RelatedCommunityId": null,
"CreatedBy": "Activity",
"SmartGroup": false,
"AndRules": null,
"OrRules": null,
"ParentGroup": null,
"EntryGroups": null,
"Hidden": false,
"ActivityType": null,
"AnyGroupList": null,
"AllGroupList": null,
"GroupType": "Default"
}
],

response deserialize using json.net returns null

I have the following json:
{
"13377": {
"id": 13377,
"orderId": 13377,
"executionStatus": "-1",
"comment": "",
"htmlComment": "",
"cycleId": -1,
"cycleName": "Ad hoc",
"versionId": 10001,
"versionName": "Version2",
"projectId": 10000,
"createdBy": "vm_admin",
"modifiedBy": "vm_admin",
"assignedTo": "user1",
"assignedToDisplay": "user1",
"assignedToUserName": "user1",
"assigneeType": "assignee",
"issueId": 10013,
"issueKey": "SAM-14",
"summary": "Test",
"label": "",
"component": "",
"projectKey": "SAM",
"folderId": 233,
"folderName": "testfolder"
}
}
I create the following classes using json2csharp and get:
public class __invalid_type__13377
{
public int id { get; set; }
public int orderId { get; set; }
public string executionStatus { get; set; }
public string comment { get; set; }
public string htmlComment { get; set; }
public int cycleId { get; set; }
public string cycleName { get; set; }
public int versionId { get; set; }
public string versionName { get; set; }
public int projectId { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
public string assignedTo { get; set; }
public string assignedToDisplay { get; set; }
public string assignedToUserName { get; set; }
public string assigneeType { get; set; }
public int issueId { get; set; }
public string issueKey { get; set; }
public string summary { get; set; }
public string label { get; set; }
public string component { get; set; }
public string projectKey { get; set; }
public int folderId { get; set; }
public string folderName { get; set; }
}
public class RootObject
{
public __invalid_type__13377 __invalid_name__13377 { get; set; }
}
When I deserialize in C# I receive no error but I get null?
Not sure how to approach this..any suggestions would be welcome.
thankyou.
Your Json object can be deserialized with an Dictionary. On this case, you can build your DataObject like you are doing:
public class Foo
{
public int id { get; set; }
public int orderId { get; set; }
...
}
And Deserialize to
Dictionary<string, Foo>
If you are using Newtonsoft:
var data = JsonConvert.DeserializeObject<Dictionary<string,Foo>(json);
You can use sometimes, when the type of object is unknown, the dynamic keyword:
var data = JsonConvert.DeserializeObject<dynamic>(json);

Portable Library Error on deserialization array of objects

I have a WebApi returning the following JSON which I am trying to deserialize to the object below
JSON OBJECT
{
"results": [{
"id": 123456,
"fullName": "Foo Bar",
"localName": null,
"jobPosition": "ME",
"jobCompanyName": "EXTRA",
"jobLocationCountry": "United States of America",
"jobLocationCity": "San Francisco",
"jobCountrySubdivision": "California",
"boards": [],
"restrictionsIndicator": false,
"personRestriction": null,
"jobRestriction": null
}, {
"id": 789101,
"fullName": "Foo Bar",
"localName": null,
"jobPosition": null,
"jobCompanyName": "Unknown",
"jobLocationCountry": "Unknown",
"jobLocationCity": "Unknown",
"jobCountrySubdivision": "Unknown",
"boards": [{
"companyId": 667525,
"companyName": "FOO BAR COMPANY",
"companyOffLimits": null,
"restrictionCategoryId": null
}
],
"restrictionsIndicator": false,
"personRestriction": null,
"jobRestriction": null
}
],
"totalCount": 2,
"pageNumber": 1,
"resultsPerPage": 100
}
C# Classes
public class Rootobject
{
public Result[] results { get; set; }
public int totalCount { get; set; }
public int pageNumber { get; set; }
public int resultsPerPage { get; set; }
}
public class Result
{
public int id { get; set; }
public string fullName { get; set; }
public object localName { get; set; }
public string jobPosition { get; set; }
public string jobCompanyName { get; set; }
public string jobLocationCountry { get; set; }
public string jobLocationCity { get; set; }
public string jobCountrySubdivision { get; set; }
public Board[] boards { get; set; }
public bool restrictionsIndicator { get; set; }
public int? personRestriction { get; set; }
public int? jobRestriction { get; set; }
}
public class Board
{
public int companyId { get; set; }
public string companyName { get; set; }
public int? companyOffLimits { get; set; }
public object restrictionCategoryId { get; set; }
}
The DLL is a Portable Class Library which is .NET 4.5 and i have JSON.net(10.0.1) installed via nuget, but the portable library is connected to a xamarin IOS Project on a mac.
If the JSON being deserialized has no Boards it works out fine but if there is a Board then I receive the following message.
Unable to find a constructor to use for type Board. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'results[1].boards[0].companyId'
The Settings I am using are:
var settings = new Newtonsoft.Json.JsonSerializerSettings
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};
I have tried the following ways to get it to serialize:
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(_jsonString, settings);
var jobject = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(_jsonString, new Rootobject());
i have tried the following
Put in a default constructor
Naming all the parameters for the class in a constructor
Adding the Attribute to the constructor
Changing the Boards to a List
Taking out the Boards Property
but there is still no joy. It will not deserialize for me.
I think you have to modify this
public class Board
{
public int companyId { get; set; }
public string companyName { get; set; }
public int? companyOffLimits { get; set; }
public object restrictionCategoryId { get; set; }
**public Board(){}**
}
also in other classes
or also change
public Board[] boards { get; set; }
to
public List<Board> boards { get; set; }
Try....

Box V2 API - Where is lock/unlock?

There is a note in the developer road map from December of 2013 saying, "Lock/Unlock – We’ve added support for locking and unlocking files into the V2 API."
I've been all through the V2 API (for c#) and cannot find it anywhere. I expected to find something in the BoxFilesManager class or as something you would pass to UpdateInformationAsync within the BoxFileRequest class.
So is there a way to lock/unlock a file?
Great question. In order to see the current lock status of a file do a
GET https://api.box.com/2.0/files/7435988481/?fields=lock
If there is no lock on the file, you'll get something like this back:
{
"type": "file",
"id": "7435988481",
"etag": "0",
"lock": null
}
If you want to lock a file, you need to do a PUT (update) on the /files/ endpoint with a body that tells us what type of lock, and when to release it. Like this:
PUT https://api.box.com/2.0/files/7435988481/?fields=lock
{"lock": {
"expires_at" : "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
You'll get a response confirming your lock was created:
{
"type": "file",
"id": "7435988481",
"etag": "1",
"lock": {
"type": "lock",
"id": "14516545",
"created_by": {
"type": "user",
"id": "13130406",
"name": "Peter Rexer gmail",
"login": "prexer#gmail.com"
},
"created_at": "2014-05-29T18:03:04-07:00",
"expires_at": "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
Since there isn't a lock/unlock yet, I created a Lock Manager based on the existing managers:
class BoxCloudLockManager : BoxResourceManager
{
#region Lock/Unlock Classes
[DataContract]
internal class BoxLockRequestInfo
{
[DataMember(Name = "status")]
public string Status { get; set; }
//[DataMember(Name = "expires_at")]
//public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
internal class BoxLockRequest
{
[DataMember(Name = "lock")]
public BoxLockRequestInfo Lock { get; set; }
}
#endregion
const string LockFileString = "{0}/?fields=lock";
public BoxCloudLockManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth)
: base(config, service, converter, auth)
{
}
public async Task<BoxLockInfo> LockAsync(string documentId,bool isDownloadPrevented = true)
{
var lockRequest = new BoxLockRequest { Lock = new BoxLockRequestInfo { Status = "lock", IsDownloadPrevented = isDownloadPrevented } };
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload(_converter.Serialize(lockRequest));
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> UnlockAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload("{\"lock\":null}");
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> GetLockInfoAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Get);
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
}
I derived a class from BoxClient, adding a LockManager and instantiate it within the Constructor.
Here is the Lock Info:
[DataContract]
public class BoxLockedBy
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "login")]
public string Login { get; set; }
}
[DataContract]
public class BoxLockDetails
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "created_by")]
public BoxLockedBy CreatedBy { get; set; }
[DataMember(Name = "created_at")]
public string CreatedAt { get; set; }
[DataMember(Name = "expires_at")]
public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
public class BoxLockInfo
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "etag")]
public string Etag { get; set; }
[DataMember(Name = "lock")]
public BoxLockDetails LockDetails { get; set; }
}