Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表单序列化总是返回空字符串_Javascript_Jquery_Ajax_Asp.net Mvc_Serialization - Fatal编程技术网

Javascript 表单序列化总是返回空字符串

Javascript 表单序列化总是返回空字符串,javascript,jquery,ajax,asp.net-mvc,serialization,Javascript,Jquery,Ajax,Asp.net Mvc,Serialization,我有一个下拉列表。根据选择,我将部分视图插入视图中的div(占位符)。下面是视图 <div class="container"> <div class="row"> <div class="col-lg-4"><p class="lead">What do you want to do?</p></div> <div class="col-lg-8"> <select

我有一个下拉列表。根据选择,我将部分视图插入视图中的div(占位符)。下面是视图

<div class="container">
    <div class="row">
    <div class="col-lg-4"><p class="lead">What do you want to do?</p></div>
    <div class="col-lg-8">
        <select id="myDropDown">
            <option id="0" selected>I want to..</option>
            <option id="1">Reset my password</option>
        </select>
    </div>
</div>
<div id="partialPlaceHolder" style="display:none;"> </div>
<script type="text/javascript">
  $(document).ready(function () {
    $('#myDropDown').change(function () {

        /* Get the selected value of dropdownlist */
        var selectedID = $(this).find('option:selected').attr('id');

        /* Request the partial view with .get request. */
        $.get('/Requests/FindPartial/' + selectedID, function (data) {

            /* data is the pure html returned from action method, load it to your page */
            $('#partialPlaceHolder').html(data);
            /* little fade in effect */
            $('#partialPlaceHolder').fadeIn('fast');
        });

    });
});

您想做什么

我想。。 重置我的密码 $(文档).ready(函数(){ $('#myDropDown')。更改(函数(){ /*获取dropdownlist的选定值*/ var selectedID=$(this).find('option:selected').attr('id'); /*使用.get请求请求局部视图*/ $.get('/Requests/FindPartial/'+selectedID,函数(数据){ /*数据是从action方法返回的纯html,请将其加载到您的页面*/ $('partialPlaceHolder').html(数据); /*小衰减效应*/ $('partialPlaceHolder').fadeIn('fast'); }); }); });

因此,当我在下拉列表中选择“重置密码”时,我成功地将部分视图插入到视图中的div中。以下是我的部分观点

 @using (Html.BeginForm())
 {
     @Html.AntiForgeryToken()
     <div class="row">
         <div class="col-lg-12">
             <div class="well well-lg" style="text-align:left">
                 <div class="form-horizontal" id="resetpasswordform">
                     <h4>Reset Password</h4>
                     <hr />
                     <div class="form-group">
                         @Html.LabelFor(model => model.ServersList, htmlAttributes: new { @class = "control-label col-md-2" })
                         <div class="col-md-10">
                             @Html.DropDownListFor(model => model.ServerName, new SelectList(Model.ServersList))
                         </div>
                     </div>
                     <div class="form-group">
                         @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
                         <div class="col-md-10">
                             @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                         </div>
                     </div>
                     <div class="form-group">
                         <div class="col-md-offset-2 col-md-10">
                             <input type="button" id="submitButton" value="Reset" class="btn btn-default" />
                         </div>
                     </div>
                 </div>
             </div>
         </div>
     </div>
 }
 <script>
     $(function () {
         $("#submitButton").click(function () {
             debugger;
             $.ajax({
                 type: "POST",
                 url: "Requests/Do_ResetPassword",
                 data: $("#resetpasswordform").serialize(),
                 success: function (data) {
                     debugger;
                 },
                 error: function (jqXHR, textStatus, errorThrown)
                 {
                     alert(errorThrown);
                 }
             });
         });
     });
 </script>
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
重置密码

@LabelFor(model=>model.ServersList,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.DropDownListFor(model=>model.ServerName,newselectlist(model.ServersList)) @LabelFor(model=>model.UserName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.UserName,new{htmlAttributes=new{@class=“form control”}) } $(函数(){ $(“#提交按钮”)。单击(函数(){ 调试器; $.ajax({ 类型:“POST”, url:“请求/Do_重置密码”, 数据:$(“#resetpasswordform”).serialize(), 成功:功能(数据){ 调试器; }, 错误:函数(jqXHR、textStatus、errorshown) { 警报(错误抛出); } }); }); });
问题是,当单击submit按钮时,我进行ajax post调用,
$(“#resetpasswordform”).serialize()
始终为“”(空字符串)

我试着只使用一个元素创建视图。我验证了元素是否具有name属性,以便序列化工作。我还确认我的按钮中没有
type=submit
。我将resetpasswordform更改为一个表单,而不是div。我甚至在Index.cshtml中直接呈现部分视图,而无需动态填充。没有任何东西能解决这个问题。它始终返回空字符串


我在SO中验证了所有其他类似的问题,没有得到任何关于我做错了什么的暗示。请帮助。

在表单标签中设置id如何:

@using (Html.BeginForm("action", "controller", FormMethod.Post, new { Id = "resetpasswordform" }))

并将其从分区中移除。

是的。。。。那就行了!!!谢谢@Qutayba。但我们如何解释这一点呢?为什么我要在htm.BeginForm中提供操作和控制器?我只是在做一个jquery调用。BeginForm只是一个有一些参数的普通方法。更多的是关于参数的顺序。您可以在JS代码中使用表单的“action”属性$(this.attr('action'))