user3128303
user3128303

Reputation: 747

Return bad value json

I have a script that sends the values ​​to the controller. Everything works fine when the value of the digit. At values ​​larger displays only the last digit. String assigned to rigidly as bad. Debugger displayed on the controller is that the value passed is correct.

script

...
$.each(data, function (i, kkk) {
    document.getElementById("sum").value = kkk
});

controller

public ActionResult Fun(int some, int some1)
{
string xxx = "18";
    if (HttpContext.Request.IsAjaxRequest())
        return Json(xxx
            , JsonRequestBehavior.AllowGet);

    return RedirectToAction("Index");
}

view

<input type="text" name="sum" id="sum"/>

displays 8...should be 18. Why is this happening? Why the return value is truncated?

Upvotes: 0

Views: 80

Answers (2)

dotnethaggis
dotnethaggis

Reputation: 999

Have a look at jQuery each function

It is setting the last character in the string to the value.

What you want to do is just set the value equal to the data returned without the loop.

I would use this:

   $.ajax({
// edit to add steve's suggestion.
url: "/ControllerName/ActionName",
success: function(data) {
     document.getElementById("sum").value = data;
}
});

Upvotes: 1

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

It looks like the result is treated as a char array. When you iterate over data, it sets the last char as the value of the input. Get rid of the loop and it should be fine.

document.getElementById("sum").value = data;

Upvotes: 1

Related Questions