Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Ajax加载的提交表单不工作_Ajax_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Ajax加载的提交表单不工作

Ajax加载的提交表单不工作,ajax,asp.net-mvc,asp.net-mvc-4,Ajax,Asp.net Mvc,Asp.net Mvc 4,我正在使用Ajax将我的页面的一部分替换为包含多个提交表单的部分视图。当我使用一个普通的Html.begin并重新加载整个页面时(提交表单加载工作),这通常是有效的。但是,当使用Ajax加载提交表单时,它们通常显示在页面上,但不起作用 我四处寻找信息,但我发现的信息主要涉及通过Ajax调用提交表单,而不是加载提交表单 这是包含javascript的主视图 @model List<SrrManager.Models.User> @{ ViewBag.Title = "Crea

我正在使用Ajax将我的页面的一部分替换为包含多个提交表单的部分视图。当我使用一个普通的Html.begin并重新加载整个页面时(提交表单加载工作),这通常是有效的。但是,当使用Ajax加载提交表单时,它们通常显示在页面上,但不起作用

我四处寻找信息,但我发现的信息主要涉及通过Ajax调用提交表单,而不是加载提交表单

这是包含javascript的主视图

@model List<SrrManager.Models.User>

@{
    ViewBag.Title = "Create";
}
<script>
    $(function () {
        $('Body').keypress(function (e) {
            if (e.which == 13) {
                $('#nomButton').trigger('click');
            }
        });
        $('#nomButton').keyup(function (e) {
            if (e.which == 13) {
                $('#nomButton').trigger('click');
            }
        });/*
        $('#prenomButton').keypress(function (e) {
            if (e.which == 13) {
                $('#nomButton').trigger('click');
            }
        });*/
        $("#nomButton").on('click', function () {

            var givenName = $("#prenomBox").val();
            var nom = $("#nomBox").val();
            $.ajax({
                url: '@Url.Action("Create", "ManageUser")', //+ "?firstname=" + givenName + "&lastname=" + lastname,
                type: 'GET',
                data: { prenom: givenName, nomFamille: nom },
                success: function (result) {
                    $('#liste-user').replaceWith(result);
                }
            });
        });
    });
</script>
<body>

    <h2>Add an user</h2>

    <p>
        <label class="label-form">Prenom :</label>@Html.TextBox("prenomBox")
        <br />
        <label class="label-form">Nom :</label> @Html.TextBox("nomBox")
        <br />
        <input type="button" value="Filter" id="nomButton" />
    </p>

    @Html.Partial("_Create", Model)

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

</body>
@型号列表
@{
ViewBag.Title=“创建”;
}
$(函数(){
$('Body')。按键(功能(e){
如果(e.which==13){
$('nomButton')。触发器('click');
}
});
$(“#nomButton”).keyup(函数(e){
如果(e.which==13){
$('nomButton')。触发器('click');
}
});/*
$(“#prenomButton”)。按键(功能(e){
如果(e.which==13){
$('nomButton')。触发器('click');
}
});*/
$(“#nomButton”)。在('单击',函数(){
var givenName=$(“#prenomBox”).val();
var nom=$(“#nomBox”).val();
$.ajax({
url:“@url.Action(“创建”、“管理用户”)”,//+“?firstname=“+givenName+”&lastname=“+lastname,
键入:“GET”,
数据:{prenom:givenName,nomFamile:nom},
成功:功能(结果){
$('#liste user')。替换为(结果);
}
});
});
});
添加用户

Prenom:@Html.TextBox(“prenomBox”)

Nom:@Html.TextBox(“nomBox”)

@Html.Partial(“_Create”,Model) @ActionLink(“返回列表”、“索引”)
创建部分视图如下所示:

@model List<SrrManager.Models.User>

<div id="liste-user">
    <table>
        <tr>
            <th>
                First Name
            </th>
            <th>
                Last Name
            </th>
            <th>
                Email
            </th>
            <th>
                Admin
            </th>
            <th></th>
        </tr>
        @foreach(var item in Model)
        {
            <tr>
                <td>
                    @item.Name
                </td>
                <td>
                    @item.LastName
                </td>
                <td>
                    @item.email
                </td>
                 @Html.Partial("__create", item)
             </tr>
        }
    </table>
</div>
@型号列表
名字
姓
电子邮件
管理
@foreach(模型中的var项目)
{
@项目名称
@item.LastName
@item.email
@Html.Partial(“\uuuu创建”,项)
}
最后一个PartialView用于创建提交表单。该模型不是一个列表,而是一个用户,在每个循环中包含一次:

@model SrrManager.Models.User


@using(Html.BeginForm()){

    <fieldset>
        <legend>User</legend>
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        @Html.HiddenFor(x => x.Name)
        @Html.HiddenFor(x => x.LastName)
        @Html.HiddenFor(x => x.email)
        @Html.HiddenFor(x => x.account)
        <td>
            @Html.EditorFor(x => x.IsAdmin)
        </td>
        <td>
            <input type="submit" value="save" />
        </td>
    </fieldset>
}
@model SrrManager.Models.User
@使用(Html.BeginForm()){
使用者
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(x=>x.Name)
@Html.HiddenFor(x=>x.LastName)
@Html.HiddenFor(x=>x.email)
@Html.HiddenFor(x=>x.account)
@EditorFor(x=>x.IsAdmin)
}
当单击Filter按钮时,通常使用Ajax调用或同步JQuery调用显示用户列表和提交表单,并按预期调用控制器方法。但是,这些按钮在加载同步JQuery调用时可以工作,但在通过Ajax加载时不能工作

控制器方法为:

public ActionResult Create(string prenom ="", string nomFamille ="")
        {
            List<User> listeUser = new List<User>();
            if (prenom.Equals("") && nomFamille.Equals(""))
            {
            }
            else if (prenom.Equals(""))
            {
                string search = "*" + nomFamille + "*";

                listeUser = SearchDirectory(search, 1);

            }
            else if (nomFamille.Equals(""))
            {
                string search = "*" + prenom + "*";

                listeUser = SearchDirectory(search, 2);
            }
            else
            {
                string search = "*" + prenom + "*" + nomFamille + "*";

                listeUser = SearchDirectory(search, 3);
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Create", listeUser);
            }
            return View(listeUser);
        }
公共操作结果创建(字符串prenom=“”,字符串nomFamille=“”) { List listeUser=新列表(); if(prenom.Equals(“”)&&nom.Equals(“”) { } else if(prenom.Equals(“”) { 字符串搜索=“*”+nomFamille+“*”; ListUser=SearchDirectory(搜索,1); } else if(nomFamille.Equals(“”) { 字符串搜索=“*”+prenom+“*”; ListUser=SearchDirectory(搜索,2); } 其他的 { 字符串搜索=“*”+prenom+“*”+nomFamille+“*”; ListUser=SearchDirectory(搜索,3); } if(Request.IsAjaxRequest()) { 返回PartialView(“创建”,listeUser); } 返回视图(ListUser); }
我是一个使用Ajax的初学者,我不知道为什么它不起作用。如果任何人有任何线索,将不胜感激

尝试为表单提交添加jQuery处理程序

$('form').submit(function () {
    var givenName = $("#prenomBox").val();
    var nom = $("#nomBox").val();

    $.ajax({
        url: '@Url.Action("Create", "ManageUser")', //+ "?firstname=" + givenName + "&lastname=" + lastname,
        type: 'GET',
        data: { prenom: givenName, nomFamille: nom },
        success: function (result) {
            $('#liste-user').html(result);
        }
    });
});

请尝试在
$中将
类型:
GET
切换到
POST
。ajax
呼叫谢谢您的时间,不幸的是它没有工作。我选择放弃提交表单:使用Ajax加载ActionLink,并将用户列表保存在控制器内存中。我认为那样比较简单。