FormData追加数组中的项目 [英] FormData append item in array

查看:125
本文介绍了FormData追加数组中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public List<Region> Regions { get; set; }

名为News.An的模型中的

区域模型是

in model called News.An Region Model is

public class Region
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public static Region Parse(DataRow row)
        {
            return new Region
            {
                Id = Database.GetInteger(row["Id"]),

                Name = Database.GetString(row["Region"]),


            };
        }
    }

在Javascript中我使用带有formdata的AJAX post方法。我想设置这个区域。

in Javascript I am using AJAX post method with formdata. I want to set this region.

var regionList = [];
            if (selected === "region") {
                if (region.length <= 0) {
                    toastr.warning('Lütfen en az bir bölge seçin !!!');
                    return;
                }

                for (var i = 0; i < region.length; i++) {

                    var item = {
                        Id: region[i]
                    }
                    regionList.push(item);
                }
                console.log(regionList);
                formData.append("Regions", regionList);
            }

JS中的代码我这样编写来设置它

Code above in JS i wrote like this to set it

 public ActionResult AddByRegion(News item)
        {
            int refPortal = SessionRepository.GetPortalId();
            if(refPortal!=1)
                return View("List", NewsRepository.ListAll(SessionRepository.GetPortalId()));
            if (item == null
                || string.IsNullOrEmpty(item.Title)
                || string.IsNullOrEmpty(item.Content)
                )
                return Content(Serialization.JsonSerialize(new { Status = 400 }));

            return Content(Serialization.JsonSerialize(new { Status = 200, Result = NewsRepository.AddByRegion(item) }));
        }

我上面的代码将进入控制器。但它总是返回0记录,但至少我选择了两个区域。

and code above i will get in controller. But it returns always 0 record although at least i choosed two region.

 $.ajax({
                type: 'POST',
                url: '@Url.Action("AddByRegion", "News")',
                data: formData,

                contentType: false,
                processData: false,
                success: function(data) {
                    var result = JSON.parse(data);
                    if (result.Result === "SUCCEED") {
                        toastr.success('@Resources.Resource.Success_MediaAdd');
                        window.location.reload();
                        return;
                    }

                    else {
                        toastr.error('@Resources.Resource.Error_Unexpected');
                        return;
                    }
                },
                error: function(error) {

                    toastr.error('@Resources.Resource.Error_Unexpected');
                    return;
                },
                beforeSend: function() {
                    waitingDialog.show('Wait...');
                },
                complete: function() {
                    waitingDialog.hide();
                }
            });

我的Ajax方法如上所述。我在哪里弄错了?

My Ajax method is above. Where am I making mistake ?

提前致谢。

推荐答案

如果您使用 FormData 发送数据,则需要 .append()每个单独的名称/值为 FORMDATA 。由于它是一个集合,您必须包含集合索引器(必须为零和连续),例如

If your using FormData to send the data, you need to .append() each individual name/value to FormData. Since its a collection, you must include the collection indexer (which must be zero based and consecutive), for example

formData.append("Regions[0].Id", someValue);
formData.append("Regions[0].Name", someValue);
formData.append("Regions[1].Id", someValue);
formData.append("Regions[1].Name", someValue);

由于你在循环中这样做,你可以使用

Since your doing this in a loop, you can use

for (var i = 0; i < region.length; i++) {
    formData.append("Regions[" + i + "].Id", region[i])
}

这篇关于FormData追加数组中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆