Bryan
Bryan

Reputation: 3541

Send array to ASP.NET from jquery and work with It

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.

Upvotes: 1

Views: 60

Answers (2)

billybob
billybob

Reputation: 2998

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
}

Upvotes: 3

Devnsyde
Devnsyde

Reputation: 1347

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"
    }
}]

Upvotes: 0

Related Questions