Search code examples
javascriptc#dotnetnuke2sxc

return Tuple to js in view


Tried using c# 7 Tuples like

public (string, bool) ProcessForm([FromBody]Dictionary<string,string> contactFormRequest)

But I get the error "CS1031: Type expected". I guess this isn't supported yet.

Then I tried

public Tuple<string, bool> ProcessForm([FromBody]Dictionary<string,string> contactFormRequest)
    {
        var message = "test";
        var result = true;
        var tuple = new Tuple<string, bool>(message, result);
        return tuple;
    }

This gives no error, but then I cannot pick it up in the view file

function handleResult(data) {
    $("#custommessages").html(data.Item1);
}

$2sxc(@Dnn.Module.ModuleID).webApi.post("Form/ProcessForm", {}, newItem, true).then(handleResult);

This outputs nothing.

If I return a simple string from the controller, "data" picks it fine.

How do you pick up the values from a Tuple return?


Solution

  • Why not create a POCO class for serialization:

    public class SomeResult
    {
        public string Message{get;set;}
        public bool Result{get;set;}
    }
    

    then

    public SomeResult ProcessForm([FromBody]Dictionary<string,string> contactFormRequest)
    {
        var message = "test";
        var result = true;
        return new SomeResult{Message = message, Result = result};
    }