jQuery Array未发送到ASP.NET MVC控制器

o0lyfsai  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(109)

我有一个困难的时间张贴从我的视图到控制器的数组列表。所有其他的价值都在传递。
有人能发现我的代码有什么问题吗?
控制器:

public ActionResult SaveProgress(BBCRMSurveyVM model)
{
    try
    {
        return Json(new { success = true });
    }
    catch (Exception e)
    {
        return Json(new { success = false, msg = e.Message });
    }
}

主视图模型:

public string ImplementationProcessFeelWasSuccessful { get; set; }
public string ImplementationProcessAdviceOtherFederations { get; set; }
public List<AdditionalTechnologyVM> AdditionalTech = new List<AdditionalTechnologyVM>();

其他TechVM:

public class AdditionalTechnologyVM
{
    public string PlatformName { get; set; }
    public string BusinessPurpose { get; set; }
    public bool? FullyIntegrated { get; set; }
    public bool? PartiallyIntegrated { get; set; }
}

JS文件:

function onAddButtonClickEvent() {
    additionaltechs.push({
       'PlatformName': platformNameAT,
       'BusinessPurpose': businessPurposeAT,
       'FullyIntegrated': integrated == "Fully" ? true : false,
       'PartiallyIntegrated': integrated == "Partially" ? true : false
    });
}

function SaveProgress() {
    var formData = $('#wizard').serializeArray();

    //if (additionaltechs.length > 0) {
    //    for (var x = 0; x < additionaltechs.length; x++) {
    //        formData[formData.length] = { name: "AdditionalTech", value: JSON.stringify(additionaltechs[x]) };
    //    }
    //}

    formData[formData.length] = { name: "AdditionalTech", value: additionaltechs };

    $.each(formData, function (index, field) {
        if (field.name.search('Budget') > 0) {
            field.value = field.value.replace('$', '').replace(/,/g, '');
        }
    });

    formData = JSON.stringify(formData);

    console.log(formData);
    
    $.ajax({
        url: '/save-progress',
        contentType: 'application/json; charset=utf-8',
        type: 'POST',
        data: formData,
        dataType: 'json',
        success: function () {},
        error: function () {}
    });
}

控制台中的输出:

列表计数始终为0?

bsxbgnwa

bsxbgnwa1#

我觉得你把事情弄得太复杂了。:)
最后,看起来你的控制器方法只接受一个模型,而不是一个数组。因此,我怀疑你发送的JSON看起来像这样:

{
 "ImplementationProcessFeelWasSuccessful" : "",
 "ImplementationProcessAdviceOtherFederations" : "",
 "AdditionalTech" : [...]
}

如果它应该是一个单一的项目被发送,那么我希望你的代码是这样的:

function SaveProgress() {

    var formData =
    {
     ImplementationProcessFeelWasSuccessful : $('#wizard #val1').val(),
     ImplementationProcessAdviceOtherFederations : $('#wizard #val2').val(),
     AdditionalTech :  additionaltechs 
         };

    formData = JSON.stringify(formData);

    console.log(formData);
    
    $.ajax({
        url: '/save-progress',
        contentType: 'application/json; charset=utf-8',
        type: 'POST',
        data: formData,
        dataType: 'json',
        success: function () {},
        error: function () {}
    });
}

但是,您发送的是一个项目数组,这在这里没有意义。

c9x0cxw0

c9x0cxw02#

最后以JSON字符串的形式发送列表,并以字符串的形式接收它。在控制器中,我像这样使用JsonConvert. sualizeObject方法:

var additionalTechs = JsonConvert.DeserializeObject<List<AdditionalTechnologyVM>> 
(model.AdditionalTech);

相关问题