0

My javascript

function SaveData(status) {
    var reserved = [];
    var id = -1;
    $('#fdEitDetail').find('table tr').each(function (index, item) {
        var isChk = $(item).find('input:checkbox').is(':checked') ? 0 : 1;
        id = $(item).attr('name');
        reserved.push({ ID: $(item).attr('id'), IsChecked: isChk, Comment: $(item).find('textarea').val() });
    });

    var dataset = JSON.stringify({ checkList: reserved, status: status, machineID: id });
    $.ajax({
        url: '/Home/UpdateData',
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        data: dataset,
        success: function (result) {
            alert(result);
        }
    });
}

I am calling this controller action:

public JsonResult UpdateData(string checkList, int status,int machineID)
{
    var response = 
        new JavaScriptSerializer().Deserialize<List<PMICheckListRequest>>(checkList);

    return Json("value = Success");
}

the class

public class PMICheckListRequest
{
    public int ID { get; set; }
    public int IsChecked { get; set; }
    public string Comment { get; set; }
}

In the controller action is being hit from the ajax request both of this parameterstatus, machineID has the value that i set in javascript but checkList is null always.

2 Answers 2

3

Just use a view model and stop hanging around those strings that you need to be parsing:

public class MyViewModel
{
    public PMICheckListRequest[] CheckList { get; set; }
    public int Status { get; set; }
    public int MachineID { get; set; }
}

as parameter to your controller action:

public ActionResult UpdateData(MyViewModel)
{
    // NO NEED TO BE DOING ANY PLUMBING DESERIALIZATION HERE
    // THE VIEW MODEL ALREADY CONTAINS EVERYTHING YOU NEED

    return Json("value = Success");
}

Notice how the view model already contains an array of PMICheckListRequest where each element has properties ID, IsChecked and Comment which you already populate in the AJAX request.

Sign up to request clarification or add additional context in comments.

Comments

2

The reason the checkList parameter is null it is because you are trying to bind a complex object JSON array (i.e [{}, {}]) to a string, when the types aren't really compatible.

Try this instead,

public JsonResult UpdateData(
    List<PMICheckListRequest> checkList, 
    int status, 
    int machineID)
{
    return Json("value = Success");
}

You do not have to deserialize it manually; the model binder will do that for you.

As a extra note, it is not a bad idea to create a view model to hold the data in a more organized way, the way @DarinDimitrov shows,

public JsonResult UpdateData(
    PMICheckListRequestViewModel model)
{
    return Json("value = Success");
}

...

public class PMICheckListRequestViewModel
{
    public List<PMICheckListRequest> CheckList { get; set; }

    public int Status { get; set; }

    public int MachineID { get; set; }
}

It will prove more flexible in the future.

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.