When a JS var has the value 0 (zero), controller API replaces it with empty field (2sxc - DNN)

73 Views Asked by At

I use 2sxc (DNN) to pass some form data into an entity trough this js:

$2sxc(@Dnn.Module.ModuleID).webApi.post("Controller/Create", {}, { 
        qtitle: "@Content.title",
        date: $todaydate,
        user: "@Dnn.User.Username",
        q1sel: $q1res,
        q1val: $q1val
        }

And this controller:

[HttpPost]
    [DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Admin)]
    [ValidateAntiForgeryToken]
    public bool Create(dynamic postController)
    {
        var quizvals = new Dictionary<string, object>();

        quizvals.Add("qtitle", postController.qtitle.ToString());
        quizvals.Add("date", postController.date.ToString());
        quizvals.Add("user", postController.user.ToString());
        quizvals.Add("q1sel", postController.q1sel.ToString());

        App.Data.Create("Quiz10q5o_entries", quizvals, "Anonymous");

        return true;
    }

I tried quite a few data types and they all work fine, with or without ToString, but when any of the $vars has a 0 (zero, JS numeric), the field is passed has empty into the entity row.

Can anyone help me understand why this happens? Is some type of conversion needed here?

Best regards, João Gomes

2

There are 2 best solutions below

2
On BEST ANSWER

I reviewed your work and it doesn't look like anything is wrong. It also seems to write 0 into the number field, as it should:

enter image description here

to me it looks like just the list-view in admin hides these values:

enter image description here

Could it be that this is all?

9
On

My guess is that you are writing to a number-field, and using a string. Try casting it to a Int32 or similar and I believe you should be good to go.