1

I have the following ViewModel:

public class CheckListInfo
{
    public int CheckListQuestionID { get; set; }
    public string QuestionText { get; set; }
    public bool Value { get; set; }
}

public class AddEditWorkOrderViewModel
{
    [Display(Name = "Work Order ID")]
    public int ID { get; set; }

    [Display(Name = "Work Order Number")]
    public string WorkOrderNumber { get; set; }

    public List<CheckListInfo> CheckListInfos = new List<CheckListInfo>();
}

and the following controller:

[HttpPost]
public ActionResult Update(int ID, AddEditWorkOrderViewModel model)
{
    if (ModelState.IsValid)
    {
        BusinessLogic.WorkOrders blWorkOrders = new BusinessLogic.WorkOrders();
        WorkOrder workOrder = blWorkOrders.GetWorkOrder(ID);

        Mapper.CreateMap<AddEditWorkOrderViewModel, DataModels.WorkOrder>();
        Mapper.Map(model, workOrder);

        blWorkOrders.UpdateWorkOrder(workOrder);

        return Json(new { success = true });
    }

    return Json(new { success = false });
}

and the following jQuery:

var checkListQuestionInfos = new Array();
var checkListQuestion = { CheckListQuestionID: 1, QuestionText: 'Test1', Value: true };
checkListQuestionInfos.push(checkListQuestion);

var data = {
    WorkOrderNumber: $("#WorkOrderNumber").val(),
    CheckListInfos: checkListQuestionInfos
}

$.ajax({
    url: '@Url.Action("Update", new { id = @Model.ID })',
    type: 'POST',
    async: false,
    contentType: "application/json; charset=utf-8", 
    data: JSON.stringify(data)
});

The ID and WorkOrderNumber is coming through just fine in my model, but the List is empty. What am I doing wrong here?

1 Answer 1

3

I figured it out. The line:

public List<CheckListInfo> CheckListInfos = new List<CheckListInfo>();

should have been

public List<CheckListInfo> CheckListInfos { get; set; }

Changing this fixed it.

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

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.