通过使用列表是空的ASP.Net MVC 3 / jQuery的AJAX(Passed List i

2019-10-17 04:22发布

我有以下视图模型:

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>();
}

和下面的控制器:

[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 });
}

及以下的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)
});

该ID和WorkOrderNumber通过就好了未来在我的模型,但该列表是空的。 我在做什么错在这里?

Answer 1:

我想到了。 该行:

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

本来应该

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

改变这种固定它。



文章来源: Passed List is empty using ASP.Net MVC 3/jQuery ajax