.net MVC的ajax提交方式总结

1 篇文章 0 订阅

一:后端通过reqeust.form方式获取值
razor页面:

@using (Html.BeginForm("SendOrder", "YongGao", FormMethod.Post, new { id = "check-form", @class = "layui-form" }))
{
    <ul class="form-left order-confirm-list">
        <li>
            <span class="form-title">领用部门:<label style="color:red;">*</label></span>
            <div class="layui-input-inline">
                <input id="ldepname" name="ldepname" class="layui-input" type="text" value="" size="30" />
                <input id="ldep" name="ldep" type="hidden">
            </div>
            
        </li>
        <li>
            <span class="form-title">领用人:<label style="color:red;">*</label></span>
            <div class="layui-input-inline">
                <input id="lpeoname" name="lpeoname" class="layui-input" type="text" value="" size="30" />
                <input id="lpeo" name="lpeo" type="hidden">
                <input id="uid" name="uid" type="hidden" value="@uid">
            </div>
        </li>
        <li>
            <a class="btn" id="id-submit-button" href="javascript://" onclick="submit_form();">提交</a>
            <a class="btn" id="id-submit-button" href="javascript://" onclick="submit_form(true);">仅采购</a>
        </li>
    </ul>
}

Controller:

		string ldep = Request.Form["ldep"];
		string uid = Request.Form["uid"];

在这里插入图片描述第二种方式:通过ajax传类对象提交,后端接收对象都是类
1.
js:

var datas = {};
var afsDetails=[{"aaa":"123"}];
datas.afsDetails = afsDetails;

$.ajax({
                type: 'POST',
                url: '@Url.Action("AfsNoticeCheck","AfterService")',
                data: datas,
                success: function (r) {
                    layer.open({
                        title: '提示'
                        ,content: r.result,
                        yes: function (index, layero) {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.location.reload();  
                            }
                    }); 
                },
                error: function (a, b, v) {
                    layer.alert($.parseJSON(a.responseText).error.message);
                }
            });
--------------------------------------------------------------------------------------
或者这样:
 $.ajax({
               type: 'POST',
               url: '@Url.Action("Check","AfterService")',
               data: {
                   "afsApplyId": $("#afsApplyId").val(),
                   "auditState": $("#auditState").val(),
                   "remark": $("#remark").val(),
                   "contractInfo":{
                       "name":$("#name").val(),
                       "mobile":$("#mobile").val(),
                       "address":$("#address").val()
                    }
           
                } ,
                success: function (r) {
                    layer.open({
                        title: '提示'
                        ,content: r.result,
                        yes: function (index, layero) {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.location.reload();  
                            }
                    }); 
                },
                error: function () {
                    alert("服务器发生错误,请联系系统管理员!");
                }
            });

----------------------------------------------------------------------------------------
或者这样
这一种自己特别记录:

```css
<form class="layui-form" action="##" id="formcheck" onsubmit="return false">
    <div style="display:none;">
        <div class="layui-input-inline" style="width:200px !important;">
            <input type="text" name="afsApplyId" id="afsApplyId" required  lay-verify="required"   class="layui-input" value="@ViewBag.id">
        </div>
    </div>
    <div class="layui-form-item" style="margin-top:10px;">
        <label class="layui-form-label">审核状态</label>
        <div class="layui-input-block" style="width:100px !important;">
            <select name="auditState" id="auditState" lay-verify="required"  lay-filter="CheckStateEvent">
                <option value="-1"></option>
                <option value="1">通过</option>
                <option value="2">驳回</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block" style="width:400px !important;">
            <textarea name="remark" id="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>

    
    <div style="display:none;" id="ApproveShow">
        <div class="layui-form-item ">
         <label class="layui-form-label">联系人:</label>
        <div class="layui-input-inline" style="width:200px !important;">
            <input type="text" name="name" id="name"  placeholder="请输入联系人"  class="layui-input">
        </div>
        <label class="layui-form-label">联系电话:</label>
        <div class="layui-input-inline" style="width:200px !important;">
            <input type="text" name="mobile" id="mobile"  placeholder="请输入联系电话"  class="layui-input">
        </div>
    </div>
        <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">联系地址</label>
        <div class="layui-input-block" style="width:400px !important;">
            <textarea name="address" id="address" placeholder="请输入联系地址" class="layui-textarea"></textarea>
        </div>
    </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="SubmitCheck()" id="CheckSubmit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="opreset">重置</button>
        </div>
    </div>
</form>

js:

var formArray = $("#formcheck").serializeArray();
 $.ajax({
               type: 'POST',
               url: '@Url.Action("Check","AfterService")',
               data: formArray,
                success: function (r) {
                    layer.open({
                        title: '提示'
                        ,content: r.result,
                        yes: function (index, layero) {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.location.reload();  
                            }
                    }); 
                },
                error: function () {
                    alert("服务器发生错误,请联系系统管理员!");
                }
            });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值