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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 BeginCollectionItem维护页面位置MVC 5_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript BeginCollectionItem维护页面位置MVC 5

Javascript BeginCollectionItem维护页面位置MVC 5,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我正试图找到一种最好的方法,在使用beginCollectionItem从列表中删除一个项目时保持页面滚动位置 起初我认为这是由于我的主项目中的其他javascript/jQuery代码(它仍然可能是一个因素),但当我在一个新项目中仅使用BCI代码(索引、两部分、控制器和模型)重新创建情况时,当从任一列表中删除一项时,页面将再次跳到顶部,我真的需要(想要)要停止这一点,我如何才能做到这一点 我已经看到了问题和我不知道如何正确实施的顶部答案,或者即使它仍然有效,第二个答案,使用我在\u布局中测试的

我正试图找到一种最好的方法,在使用beginCollectionItem从列表中删除一个项目时保持页面滚动位置

起初我认为这是由于我的主项目中的其他javascript/jQuery代码(它仍然可能是一个因素),但当我在一个新项目中仅使用BCI代码(索引、两部分、控制器和模型)重新创建情况时,当从任一列表中删除一项时,页面将再次跳到顶部,我真的需要(想要)要停止这一点,我如何才能做到这一点

我已经看到了问题和我不知道如何正确实施的顶部答案,或者即使它仍然有效,第二个答案,使用我在
\u布局
中测试的以下JS,在
budles
中,它没有解决问题

jQuery测试

$(document).scroll(function () {
    localStorage['page'] = document.URL;
    localStorage['scrollTop'] = $(document).scrollTop();
});
$(document).ready(function () {
    if (localStorage['page'] == document.URL) {
        $(document).scrollTop(localStorage['scrollTop']);
    }
});
学生部分

@model UsefulCode.Models.Person
<div class="editorRow">
    @using (Html.BeginCollectionItem("students"))
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>
                    @Html.TextBoxFor(m => m.firstName)
                </span>
            </div>
            <div class="ui-block-b">
                <span>
                    @Html.TextBoxFor(m => m.lastName)
                </span>
            </div>
            <div class="ui-block-c">
                <span>
                    <span class="dltBtn">
                        <a href="#" class="deleteRow">X</a>
                    </span>
                </span>
            </div>
        </div>
    }
</div>
@model UsefulCode.Models.Person
<div class="editorRow">
    @using (Html.BeginCollectionItem("teachers"))
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>
                    @Html.TextBoxFor(m => m.firstName)
                </span>
            </div>
            <div class="ui-block-b">
                <span>
                    @Html.TextBoxFor(m => m.lastName)
                </span>
            </div>
            <div class="ui-block-c">
                <span>
                    <span class="dltBtn">
                        <a href="#" class="deleteRow">X</a>
                    </span>
                </span>
            </div>
        </div>
    }
</div>
@model UsefulCode.Models.Person
@使用(Html.BeginCollectionItem(“学生”))
{
@Html.TextBoxFor(m=>m.firstName)
@Html.TextBoxFor(m=>m.lastName)
}
教师部分

@model UsefulCode.Models.Person
<div class="editorRow">
    @using (Html.BeginCollectionItem("students"))
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>
                    @Html.TextBoxFor(m => m.firstName)
                </span>
            </div>
            <div class="ui-block-b">
                <span>
                    @Html.TextBoxFor(m => m.lastName)
                </span>
            </div>
            <div class="ui-block-c">
                <span>
                    <span class="dltBtn">
                        <a href="#" class="deleteRow">X</a>
                    </span>
                </span>
            </div>
        </div>
    }
</div>
@model UsefulCode.Models.Person
<div class="editorRow">
    @using (Html.BeginCollectionItem("teachers"))
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>
                    @Html.TextBoxFor(m => m.firstName)
                </span>
            </div>
            <div class="ui-block-b">
                <span>
                    @Html.TextBoxFor(m => m.lastName)
                </span>
            </div>
            <div class="ui-block-c">
                <span>
                    <span class="dltBtn">
                        <a href="#" class="deleteRow">X</a>
                    </span>
                </span>
            </div>
        </div>
    }
</div>
@model UsefulCode.Models.Person
@使用(Html.BeginCollectionItem(“教师”))
{
@Html.TextBoxFor(m=>m.firstName)
@Html.TextBoxFor(m=>m.lastName)
}
索引

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model UsefulCode.Models.Register
<div id="studentList">
@using (Html.BeginForm())
{
    <div id="editorRowsStudents">
        @foreach (var item in Model.students)
        {
            @Html.Partial("StudentView", item)
        }
    </div>
    @Html.ActionLink("Add", "StudentManager", null, new { id = "addItemStudents", @class = "button" });
}
</div>

<div id="teacherList">
@using (Html.BeginForm())
{
    <div id="editorRowsTeachers">
        @foreach (var item in Model.teachers)
        {
            @Html.Partial("TeacherView", item)
        }
    </div>
    @Html.ActionLink("Add", "TeacherManager", null, new { id = "addItemTeachers", @class = "button" });
}
</div>


@section scripts {
    <script type="text/javascript">
    $(function () {
        $('#addItemStudents').on('click', function () {
            $.ajax({
                url: '@Url.Action("StudentManager")',
                    cache: false,
                    success: function (html) { $("#editorRowsStudents").append(html); }
                });
                return false;
            });
            $('#editorRowsStudents').on('click', '.deleteRow', function () {
                $(this).closest('.editorRow').remove();
            });
            $('#addItemTeachers').on('click', function () {
                $.ajax({
                    url: '@Url.Action("TeacherManager")',
                    cache: false,
                    success: function (html) { $("#editorRowsTeachers").append(html); }
                });
                return false;
            });
            $('#editorRowsTeachers').on('click', '.deleteRow', function () {
                $(this).closest('.editorRow').remove();
            });
        });
    </script>
}
@{
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@model UsefulCode.Models.Register
@使用(Html.BeginForm())
{
@foreach(Model.students中的var项)
{
@Html.Partial(“StudentView”,项目)
}
@ActionLink(“Add”,“StudentManager”,null,新的{id=“addItemStudents”,@class=“button”});
}
@使用(Html.BeginForm())
{
@foreach(Model.teachers中的var项)
{
@Html.Partial(“教师视图”,项目)
}
@ActionLink(“Add”,“TeacherManager”,null,新的{id=“addItemTeachers”,@class=“button”});
}
@节脚本{
$(函数(){
$('#addItemStudents')。在('click',函数(){
$.ajax({
url:“@url.Action”(“StudentManager”),
cache:false,
成功:函数(html){$(“#editorRowsStudents”).append(html)}
});
返回false;
});
$('#editorRowsStudents')。在('click','.deleteRow',函数(){
$(this).closest('.editorRow').remove();
});
$('#addItemTeachers')。在('click',函数(){
$.ajax({
url:“@url.Action”(“TeacherManager”),
cache:false,
成功:函数(html){$(“#editorRowsTeachers”).append(html)}
});
返回false;
});
$('editorRowsTeachers')。on('click','deleteRow',函数(){
$(this).closest('.editorRow').remove();
});
});
}

除了给自己造成痛苦之外,我现在感觉自己松了一口气。 添加
返回false到删除操作解决了这个问题,它出现在添加操作上,但由于长时间盯着它看,我认为它就在那里

如果有更好的答案,请输入,因为这只适用于这段代码

以下是答案代码:

$('#addItemStudents').on('click', function () {
            $.ajax({
                url: '@Url.Action("StudentManager")',
                    cache: false,
                    success: function (html) { $("#editorRowsStudents").append(html); }
                });
                return false;
            });
            $('#editorRowsStudents').on('click', '.deleteRow', function () {
                $(this).closest('.editorRow').remove();
                return false; // add this
            });