What parameter should I pass to Editor.Process() when using json data

What parameter should I pass to Editor.Process() when using json data

Jack TingJack Ting Posts: 14Questions: 3Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
1. I'm developing a c# asp.net core web api application.
2. I copy the StaffController and StaffModel from Editor-NETCore-2.0.8 and test it OK, with the defult 'Form' action.
3. But, I want to send an application/json request instead of multipart/form-data request.
4. I've read the discussion https://datatables.net/forums/discussion/55370/custom-insert-update-delete-method-for-editor. which is almost the same of my case.

The problem is when I do a POST api/staff with JSON data (shown below), it just return all the data just like a GET api/staff do.

{
"action":"edit",
   "data":{
      "DT_RowId": "row_60",
      "first_name": "test1",
      "last_name": "test2",
      "extn": "1564",
      "age": null,
      "salary": 95000.0,
      "start_date": "2022-06-10",
      "position": "test",
      "email": "",
      "office": "test"
   }
}

So, my question is what parameter should I passed to Editor.Process( ?? ) instead of Editor.Process( Request )

Answers

  • Jack TingJack Ting Posts: 14Questions: 3Answers: 0

    Currently I leave the parameter of public ActionResult Staff() to be blank, should I filled something in it?
    for example: public ActionResult Staff([FromBody] ?? request)

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    I don't actually think that is currently possible without transforming the JSON data in some way. There are a number of different overloads for Process but none of them would accept a JSON string / object.

    Can I ask why you want to sent it as JSON in the body rather than form data?

    Allan

  • Jack TingJack Ting Posts: 14Questions: 3Answers: 0

    Can I ask why you want to sent it as JSON in the body rather than form data?

    It's specification from a RPF owner.

    From the following

        public class PostStaffModel {
            public string action { get; set; }
            public List<StaffModel> data { get; set; }
        }
    

    and

    [Route("api/staff")]
            [HttpPost]
            public ActionResult PostStaff(PostStaffModel req) {
    

    I get the req object correctly,
    Is there still no way to processing this req object??

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    No, I'm afraid not. What is really needed is to get the incoming data into a DtRequest object which is what the libraries use to access the submitted data.

    If you can get your JSON as a IEnumerable<KeyValuePair<string, string>> then you could pass it through the HttpData method of that class to get the resulting object, and then use that with Editor.Process().

    Allan

  • Jack TingJack Ting Posts: 14Questions: 3Answers: 0

    I try to make use of IEnumerable<KeyValuePair<string, string>>, but still fail to get correct response.

    the Model:

        public class PostModel<T> {
            public string action { get; set; }
            public T data { get; set; }
        }
    

    the controller:

            [Route("api/staff")]
            [HttpPost]
            public ActionResult PostStaff(PostModel<StaffModel> req) {
                var request = myConvert(req);
    

    and myConvert()

            private List<KeyValuePair<string, string>> myConvert(PostModel<StaffModel> req) {
                var jStr = JsonConvert.SerializeObject(req);
                var dObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(jStr);
                var oData = new List<KeyValuePair<string, string>>();
                if (dObj != null) {
                    foreach (KeyValuePair<string, object> kv in dObj) {
                        oData.Add(new KeyValuePair<string, string>(kv.Key, kv.Value.ToString() ));
                    }
                }
                return oData.ToList();
            }
    

    in trouble is at line 259 of DtRequest.cs

    private void _Build(IEnumerable<KeyValuePair<string, string>> rawHttp, string culture)
    {
        var http = HttpData(rawHttp, culture);
            ...
            if (Action == "create")
            {
                RequestType = RequestTypes.EditorCreate;
                Data = http["data"] as Dictionary<string, object>;
            }
    

    after this line Data = http["data"] as Dictionary<string, object>;
    the Data get a null.

    I'm wondering where the error comes from ? the HttpData()? or
    What I was trying is wrong?

  • Jack TingJack Ting Posts: 14Questions: 3Answers: 0

    OK, I trace down into HttpData() which use _HttpConv() to convert string into various type of data value, but it does not process { and [ at all.

    I check the function _Build(), the main part of it actually make use of variable http -- an Dictionary<string, object>.

    So, why not move the first line of it var http = HttpData(rawHttp, culture); out where who calls it. And then we can have another overload of function DtRequest() ?

    public DtRequest(Dictionary<string, object> jsObj) {
        _Build(jsObj);
    }
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Doesn't that fall under this constructor? Dictionary is IEnumerable<KeyValuePair<TKey,TValue>> - perhaps it is just the TValue that is the issue, it should be an object...?

    Allan

Sign In or Register to comment.