欢迎关注爱分享
分享各种软件,学习资料,提供odoo源码下载

.Net MVC(一) JQuery serialize()使用方法

用ajax往后端传数据时,以前获取form的数据都是直接获取text的值

username: $('#username').val(),
password: $('#password').val()

如果form里的字段比较多的话,用这种方式并不合适,此时,就可以使用serialize()函数。ajax代码如下:

$("#save").click(function () {
        $.ajax({
            url: "/home/saveequeipment",
            type: "post",
            dateType: "json",        
            data: $('#addequipment').serialize(),
            success: function (data) {               
            }
        })
    });

serialize对应的是获取表单里的name属性值。

对应html代码:

 <form class="form-inline" id="addequipment">
                        <!-- PAGE CONTENT BEGINS -->
                        <div style="padding: 10px">
                            <div class="form-group " style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">部门名称</label>
                                <input type="text" class="form-control" id="EQUIP_TYPE" name="EQUIP_TYPE" placeholder="部门名称" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">资产卡片号</label>
                                <input type="text" class="form-control" id="EQUIP_CODE" name="EQUIP_CODE" placeholder="资产卡片号" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">资产名称</label>
                                <input type="text" class="form-control" id="EQUIP_NAME" name="EQUIP_NAME" placeholder="资产名称" required="required">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">规格</label>
                                <input type="text" class="form-control" id="LABEL" name="LABEL" placeholder="规格" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">品牌</label>
                                <input type="text" class="form-control" id="FACTORY" name="FACTORY" placeholder="品牌" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">所在位置</label>
                                <input type="text" class="form-control" id="LOCATION" name="LOCATION" placeholder="所在位置" required="required">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">入库日期</label>
                                <input type="text" class="form-control" id="BUY_TIME" name="BUY_TIME" placeholder="入库日期" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">使用状态名称</label>
                                <input type="text" class="form-control" id="CONDITION" name="CONDITION" placeholder="使用状态名称" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">自定义编码</label>
                                <input type="text" class="form-control" id="REGEST_CODE" name="REGEST_CODE" placeholder="自定义编码">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">供应商名称</label>
                                <input type="text" class="form-control" id="EQUIP_COUNTRY" name="EQUIP_COUNTRY" placeholder="供应商名称" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">设备原值</label>
                                <input type="text" class="form-control" id="EQUIP_MON" name="EQUIP_MON" placeholder="设备原值" required="required">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">合同编号</label>
                                <input type="text" class="form-control" id="EQUIP_HT" name="EQUIP_HT" placeholder="合同编号" required="required">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">备注</label>
                                <input type="text" class="form-control" id="MEMO" name="MEMO" placeholder="备注">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">国家名称</label>
                                <input type="text" class="form-control" id="EQUIP_GUOJIA" name="EQUIP_GUOJIA" placeholder="国家名称">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">SN</label>
                                <input type="text" class="form-control" id="SN" name="SN" placeholder=" SN" required="required">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">科研金额</label>
                                <input type="text" class="form-control" id="KEYAN_MON" name="KEYAN_MON" placeholder="科研金额">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">自筹金额</label>
                                <input type="text" class="form-control" id="ZICHOU_MON" name="ZICHOU_MON" placeholder="自筹金额">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">财政金额</label>
                                <input type="text" class="form-control" id="CAIWU_MON" name="CAIWU_MON" placeholder="财政金额">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">捐赠金额</label>
                                <input type="text" class="form-control" id="JUANKUAN_MON" name="JUANKUAN_MON" placeholder="捐赠金额">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">教学金额</label>
                                <input type="text" class="form-control" id="JIAOXUE_MON" name="JIAOXUE_MON" placeholder="教学金额">
                            </div>
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputEmail2" style="width: 90px">卡片位置名称</label>
                                <input type="text" class="form-control" id="KAPIAN_LOCATION" name="KAPIAN_LOCATION" placeholder="卡片位置名称">
                            </div>
                        </div>
                        <div style="padding: 10px">
                            <div class="form-group" style="width: 300px">
                                <label for="exampleInputName2" style="width: 90px">责任人名称</label>
                                <input type="text" class="form-control" id="MANAGE_PEO" name="MANAGE_PEO" placeholder="责任人名称" required="required">
                            </div>
                        </div>
                        <div class="clearfix form-actions">
                            <div class="col-md-offset-3 col-md-9">
                                <button class="btn btn-info" type="submit" id="save">
                                    <i class="ace-icon fa fa-check bigger-110"></i>
                                    提交
                                </button>
                                &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
											<button class="btn" type="reset">
                                                <i class="ace-icon fa fa-undo bigger-110"></i>
                                                重置
                                            </button>
                            </div>
                        </div>

                    </form>

后端接受处理方法:

[HttpPost]
        public ActionResult saveequeipment(ParaClass p)
        {

           
            return Content(p.EQUIP_TYPE);
           
        }

使用ParaClass加载对应字段,ParaClass是Model中字段的构造函数,如下显示:

 public class ParaClass
    {
        public string EQUIP_TYPE { get; set; }
        public string EQUIP_CODE { get; set; }
        public string EQUIP_NAME { get; set; }
        public string LABEL { get; set; }
        public string FACTORY { get; set; }
        public string LOCATION { get; set; }
        public string CONDITION { get; set; }
        public string REGEST_CODE { get; set; }
        public string EQUIP_COUNTRY { get; set; }
        public string EQUIP_MON { get; set; }
        public string EQUIP_HT { get; set; }
        public string MEMO { get; set; }
        public string EQUIP_GUOJIA { get; set; }
        public string SN { get; set; }
        public string KEYAN_MON { get; set; }
        public string ZICHOU_MON { get; set; }
        public string CAIWU_MON { get; set; }
        public string JUANKUAN_MON { get; set; }
        public string JIAOXUE_MON { get; set; }
        public string KAPIAN_LOCATION { get; set; }
        public string MANAGE_PEO { get; set; }
        public DateTime BUY_TIME { get; set; }
    }

由于次项目没有使用ORM,以上字段都是人为输入。有ORM的话,字段可自动映射完成。

赞(0) 打赏
未经允许不得转载:iShare » .Net MVC(一) JQuery serialize()使用方法
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

iShare-爱分享 更专业 爱分享

Odoo下载联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏