使用js数组拼接json并post到contoller

view:

<script type="text/javascript">

    var id = 0;
    var i = 1;
    var s = ["div1"];
    function add() {
        i++;
        var id = "div" + i.toString();
        s.push(id);
        var t = '<div id="div' + i.toString() + '" style="border: 1px dashed #F00;margin:10px" >' +
        '<input type="text" class="input-text" style="width: 180px;margin:5px" value="" name="test"/>' +
        '<input type="text" class="input-text" style="width: 180px;margin:5px" value="" name="test"/>' +
        '<input type="text" class="input-text" style="width: 180px;margin:5px;float:none;" value="" name="test" />' +
        '<input type="text" class="input-text" style="width: 180px;margin:5px" id="field_2" value="" name="test" />' +
            '<div style="background-image:url(/images/add.png);width:40px;height:40px;float:left" onclick="add()"></div>' +
            '<div style="background-image:url(/images/delect.png);width:40px;height:40px;float:right" onclick="remove(div' + i.toString() + ')"></div></div>';
        $(divList).append(t);
    }
    function remove(id) {
        id.parentNode.removeChild(id);
    }

    function article_save_submit() {

        var jsonstr = "";
        var a = [];
        var Pname = document.getElementById("txt_pjName").value;
        var PjCity = document.getElementById("txt_pjCity").value;
        var MachineNum = document.getElementById("txt_mchineNum").value;
        var IsNow = document.getElementById("select_IsNow").value;
        var PjXSName = document.getElementById("txt_PjXSName").value;
        var IsDealer = document.getElementById("txt_IsDealer").value;
        var Phone = document.getElementById("txt_phone").value;
        var Other = document.getElementById("txt_other").value;
        debugger
        var url = '@Url.Action("InserProject", "PreAdmin")';


        a.push({ 'Pname': Pname, 'PjCity': PjCity, 'MachineNum': MachineNum, 'IsNow': IsNow,'PjXSName': PjXSName, 'IsDealer': IsDealer, 'Phone': Phone, 'Other': Other })
        for (var i = 0; i < s.length; i++) {
            var divname = "#" + s[i] + "  input[type='text']";
            var x = 1;
            var PjName = "";
            var PjNum = "";
            var Xdd = "";
            var Xddw = "";
            $(divname).each(function () {
                if (x == 1) {
                    PjName = $(this).val();
                }
                if (x == 2) {
                    PjNum = $(this).val();
                }
                if (x == 3) {
                    Xdd = $(this).val();
                }
                if (x == 4) {
                    Xddw = $(this).val();
                }

                x++;

            });
            a.push({ 'PjName': PjName, 'PjNum': PjNum, 'Xdd': Xdd, 'Xddw': Xddw })
        }
        jsonstr = JSON.stringify(a);
        alert(jsonstr)

        var url = '@Url.Action("InserMachineInfo", "PreAdmin")';
        $.post(url, { jsonstr: jsonstr, id: id },
         function (istrue) {
         });
    }

controller(use ef) :

[HttpPost]
        public void InserMachineInfo(string jsonstr,int id)
        {
            //use json.net
            JArray o = (JArray)JsonConvert.DeserializeObject(jsonstr);
            IList<JToken> oList = (IList<JToken>)o;
            int proId=0;
            foreach (JToken jt in oList)
            {

                JObject jo = jt as JObject;
                using (var db = new TestTryEntities1())
                {
                    if (jt == o[0])
                    {
                        ProjectInfo pro = new ProjectInfo()
                        {
                            pname = jo["Pname"].ToString(),
                            pjCity = jo["PjCity"].ToString(),
                            machineNum = jo["MachineNum"].ToString(),
                            isNow = Convert.ToInt32(jo["IsNow"].ToString()),
                            pjXSName = jo["PjXSName"].ToString(),
                            isDealer = Convert.ToInt32(jo["IsDealer"].ToString()),
                            phone = jo["Phone"].ToString(),
                            other = jo["Other"].ToString(),
                            createtime = DateTime.Now,
                            fixtime = DateTime.Now
                        };
                        db.ProjectInfo.Add(pro);
                        db.SaveChanges();
                        proId = pro.id;
                    }
                    else 
                    {
                        machineinfo model = new machineinfo()
                        {
                            id = proId,
                            machineName = jo["PjName"].ToString(),
                            machineNum = Convert.ToInt32(jo["PjNum"].ToString()),
                            test = jo["Xdd"].ToString(),
                            other = jo["Xddw"].ToString(),
                            createtime = DateTime.Now,
                            fixtime = DateTime.Now

                        };
                        db.machineinfo.Add(model);
                        db.SaveChanges();
                    }

                }
            }
        }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lz37025/article/details/74936451
文章标签: json javascript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

使用js数组拼接json并post到contoller

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭