1

I have en array that looks like this:

[Object { OldData="(3) Lindrigt skadad",  NewData="(9) Uppgift saknas",  AccidentNumber=1173590}]

I make a Jquery-post as below to ASP.NET:

        $.ajax({
            type: "POST",
            url: DataReview.BASE + "/UOS/SaveUOSChangeLog",
            data: postData,
            success: function (data) {
                //alert(data.Result);
            },
            dataType: "json",
            traditional: true
        });

Here Is my controller:

public ActionResult SaveUOSChangeLog(List<String> values)
{
    try
    {
        var fish = Json(new { Data = values });
        return Json(new { Result = "True", ResultData = values }, JsonRequestBehavior.AllowGet);
    }
    catch(Exception e)
    {
        return Json(new { Result = "Fail", Message = "Faaaaaail" }, JsonRequestBehavior.AllowGet);
    }
}

When I debug this, the value of values is [0] = "[object Object]"

How can I access the actually values from the array?

EDIT:

I have created the following model:

   public class UOSChangeLogFrontEnd
    {
        public int AccidentNumber { get; set; }
        public string OldData { get; set; }
        public string NewData { get; set; }
        public int Action { get; set; }
    }

An my controller looks like this:

public ActionResult SaveUOSChangeLog(List<UOSChangeLogFrontEnd> values)
        {
            try
            {
                var fish = Json(new { Data = values });
                return Json(new { Result = "True", ResultData = values }, JsonRequestBehavior.AllowGet);
            }
            catch(Exception e)
            {
                return Json(new { Result = "Fail", Message = "Faaaaaail" }, JsonRequestBehavior.AllowGet);
            }
        }

But the value count Is 0 when I debug.

4
  • 1
    The object that you are passing doesn't appear to be a list of values, what if you made a model class that reflects the data you are sending to that controller and then use the class as your parameter for SaveUOSChangelog Commented Mar 15, 2016 at 14:53
  • 1
    The list of values should not be a String. Create a Model and use it instead Commented Mar 15, 2016 at 14:55
  • @Noctane: So I should make a model class with properties that corresponds to the properties in my array? Commented Mar 15, 2016 at 14:56
  • @Bryan Yes. That should do it. Commented Mar 15, 2016 at 15:01

2 Answers 2

3

Create a model like this, instead of using String as a model.

public class AccidentModel
{
    public int AccidentNumber { get; set; }
    public string OldData { get; set; }
    public string NewData { get; set; }
}

Then used it in your action like this:

public ActionResult SaveUOSChangeLog(AccidentModel accident)
{
    //..use your model
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. But I can't get It work. Check my updated question.
What's the data in your javascript. Put a breakpoint to check it.
And try to specify the contentType in your ajax call. Something like this: contentType: "application/json; charset=utf-8",
@billybop: What do u mean? When I do a console.log on the array that is posted, It looks like this: [Object { OldData="(3) Lindrigt skadad", NewData="(11) Avlidit av sjukdom", AccidentNumber=1173590, more...}]
billybob: I solved It by setting: data: JSON.stringify({ "values": TableChanges }),
|
0

Try this:

Model:

public class Object
{
    public string OldData { get; set; }
    public string NewData { get; set; }
    public string AccidentNumber { get; set; }
}
public class RootObject
{
    public Object Object { get; set; }
}

Controller:

public ActionResult SaveUOSChangeLog(List<RootObject> values)

JavaScript:

[{
    "Object": {
    "OldData": "(3) Lindrigt skadad",
    "NewData": "(9) Uppgift saknas",
    "AccidentNumber": "1173590"
    }
}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.