使用Ajax从此模型更新数据库

时间:2015-06-10 04:25:49

标签: jquery ajax json asp.net-mvc

我在视图中使用了Ajax功能。

功能保存(){

    var Url = '@Url.Action("UpdateCaseDetails", "CaseDetailView")';
    var frm = $("form");
    var data = JSON.stringify(frm.serializeArray());
    $.ajax({
        url: Url,
        type: "POST",
        dataType: "json",
        data: { models: data },
        async: false,
        cache:true,
        success: function (data) {
            alert(JSON.stringify(data));
        }
    });
}

我的控件有此代码

[HttpPost]
        public ActionResult UpdateCaseDetails(string models)
        {
            string errorMsg = string.Empty;
            string successMsg = string.Empty;
            string testModel = models;
            CaseDetailViewModel obj = JsonConvert.DeserializeObject<CaseDetailViewModel>(testModel);
            successMsg = "Case update successfully";
            return Json(new { success = true, message = successMsg });
        }

我需要使用Ajax从此模型更新数据库。在使用form.serializeArray()方法发布视图后,它会在json字符串后发布。

[{"name":"CaseAction","value":"10"},{"name":"CaseHoldsFor","value":"0"},
{"name":"CaseHoldUntil","value":"0001-01-01"},
{"name":"CaseOfficerAssigned","value":""},
{"name":"CaseDateToLegal","value":"0001-01-01 12:00:00 AM"},
{"name":"CaseDateToCourt","value":"0001-01-01"},
{"name":"CaseCourtCaseNo","value":" "},
{"name":"CaseDateHere","value":"0001-01-01"}]

但是在点击此代码之后会出现以下错误。

CaseDetailViewModel obj = JsonConvert.DeserializeObject<CaseDetailViewModel>(testModel);

错误详情:

    Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RAMIS.Collection.BusinessEntity.Models.CaseDetailViewModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 
To fix this error either change the JSON to a JSON object 
(e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

我也尝试了以下方式。

CaseDetailViewModel objResponse1 = JsonConvert.DeserializeObject<CaseDetailViewModel>(testModel.Substring(1, testModel.Length - 2));

但它对我不起作用。请给我一个解决方案

编辑:

我的Model类如下:

public class CaseDetailViewModel
    {            
        [DataMember]
        public string Tin { get; set; }    
        [DataMember]
        public string CaseID { get; set; }    
        [DataMember]
        public string CaseType { get; set; }    
        [DataMember]
        public string CaseStatus { get; set; }    
        [DataMember]
        public string CaseTaxType { get; set; }    
        [DataMember]
        public DateTime? CaseTaxPeriodBegin { get; set; }            
        [DataMember]
        public DateTime? CaseTaxPeriodEnd { get; set; }    
        [DataMember]
        public DateTime? CaseCreationDate { get; set; }    
        [DataMember]
        public decimal? CaseBalance { get; set; }    
        [DataMember]
        public string CaseOfficerAssigned { get; set; }    
        [DataMember]
        public DateTime? CaseDateAssigned { get; set; }    
        [DataMember]
        public int CaseAgeofCase { get; set; }    
        [DataMember]
        public string CaseCustomerName { get; set; }
}

2 个答案:

答案 0 :(得分:0)

由于您的json数据是键值对的数组,您可能需要将此json数据反序列化为关键值对的字典,如下所示。

ssh -vvv git@gitlab.com:username/reponame.git

答案 1 :(得分:0)

如果你想使用实体CaseDetailViewModel,你需要在javascript上创建一个等于CaseDetailViewModel的结构并将其串行化。

var struct = [];
struct.push (new Object ({
Tin: ""
CaseID: ""
CaseType: ""
CaseStatus: ""
CaseTaxType: ""
CaseTaxPeriodBegin: null,
CaseTaxPeriodEnd: null,
CaseCreationDate: null,
CaseBalance: null,
CaseOfficerAssigned: ""
CaseDateAssigned: null,
CaseAgeofCase: 1,
CaseCustomerName: ""
}));
jQuery.ajax ({
url: ""
type: "post"
asyn: true,
dataType: "json"
data: {vm: JSON.stringify (struct)}});

[HttpPost]
public ActionResult UpdateCaseDetails (string vm)
{
var obj = JsonConvert.DeserializeObject <IEnumerable<CaseDetailViewModel>> (vm);
}

编辑: 如果您只需要使用一个对象:

var struct = new Object(..... complete object

var obj = JsonConvert.DeserializeObject <CaseDetailViewModel>(vm);