简体   繁体   English

Razor 将页面返回到 Post 方法中的特定位置

[英]Razor Return Page to a specific location in Post Method

So idea is Id like the user to click a button, have the page submit through the post method which will populate fields for a form, then return the page with a modal displayed.所以想法是我喜欢用户单击一个按钮,通过 post 方法提交页面,该方法将填充表单的字段,然后返回显示模式的页面。 I have this working, except the issue is that the page is at the top even if the user clicked halfway down.我有这个工作,除了问题是页面在顶部,即使用户点击了一半。 Id like the page to redisplay where it was, with the modal displaying.我希望页面重新显示它所在的位置,并显示模式。

I know that there is a fragment parameter, but only for redirecttopage and not page()?我知道有一个片段参数,但仅适用于redirecttopage 而不是page()? Sending it back to get resets everything, so thats not realy ideal.将其送回以重置所有内容,因此这并不理想。 Ive also tried altering the url with javascript which works with setting the page position, but somehow it will not display the modal with that?我还尝试使用 javascript 更改 url,该 url 可用于设置页面位置,但不知何故它不会显示模式?

Does anyone know how to easily return the page in the post method to a specific spot on the page?有谁知道如何轻松地将 post 方法中的页面返回到页面上的特定位置?

Javascript: (Modal shows up for one second then disappers when it goes to the page position) Javascript:(模态显示一秒钟,然后在到达页面位置时消失)

        function OnLoad() { //Used for redisplaying on error purposes
            if ("@TempData["EditingParty"]" == "Yes") {

                var loadedURL = window.location.href;
                var fragmentPieceOfURL = "@TempData["EndOfURL"]"

                location.href = loadedURL + fragmentPieceOfURL;

                document.getElementById("EditUsersForm").style.display = "inline-block";


            }
            else {
                document.getElementById("EditUsersForm").style.display = "none";
            }


    }

Cshtml Button that goes to post (i is counter to keep track of anchor tag for redisplay location):用于发布的 Cshtml 按钮(i 是跟踪锚标记以重新显示位置的计数器):

<a id="@i"></a>

<button type="submit" style="white-space:nowrap" class="btn btn-secondary btn-sm" formnovalidate asp-page-handler="DisplayEdit" asp-route-id="@Model.UsersOnCaseList.ElementAtOrDefault(i).UserID" asp-route-AttorneyRole="@Model.UsersOnCaseList.ElementAtOrDefault(i).AttorneyRole" asp-route-email="@Model.UsersOnCaseList.ElementAtOrDefault(i).EmailAddress" asp-route-pAttorneyRoleID="@Model.UsersOnCaseList.ElementAtOrDefault(i).AttorneyRoleID" asp-route-PageFragment="@i" asp-route-pEditAttorneyUserID="@Model.UsersOnCaseList.ElementAtOrDefault(i).UserID">Edit <i class="fas fa-ribbon"></i></button>

Modal:模态:

<div class="modal" tabindex="-1" role="dialog" id="EditUsersForm" style="display:none">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit User</h5>
                        <button type="button" onclick="closeEditUsersForm()" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="form-group" style="margin-top:15px;padding-left:15px">
                        <label asp-for="EditUserEmailInput"></label> <span class="red">*</span>
                        <input asp-for="EditUserEmailInput" class="form-control" id="EmailInputBox" value="@Model.EditUserEmailInput" style="display:block;width:25em" />
                        <span style="display:block" asp-validation-for="EditUserEmailInput" class="text-danger" id="AddUserInputValidation"></span>

                    </div>
                    <div class="form-group" style="margin-top:15px;padding-left:15px">
                        <label class="control-label">Role</label> <span class="red">*</span>
                        <select asp-for="AttorneyRoleIDEditForm" asp-items="@(new SelectList(Model.AttorneyRolesList, "AttorneyRoleID","AttorneyRole"))" name="AttorneyRoleIDEditForm" id="SelectAttorneyRoleIDEditForm" class="form-control" style="width:25em" onchange="DidSelectPlaintiffEdit()">
                            <option class="form-control" value=-1>Select a Role</option>
                        </select>
                        <span asp-validation-for="AttorneyRoleIDEditForm" class="text-danger"></span>
                    </div>
                    <div class="form-group" style="margin-top:15px;padding-left:15px">
                        <label asp-for="EditUserPartyInput"></label>  <span class="red">*</span><br />
                        <input asp-for="EditUserPartyInput" value="@Model.EditUserPartyInput" class="form-control" id="PartyNameInputEdit" style="display:inline-block;width:25em" />
                        <span style="display:block" asp-validation-for="EditUserPartyInput" class="text-danger" id="PartyNameInputValidation"></span>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary" style="margin:0 auto" asp-page-handler="SaveUserEdits" asp-route-UserBeingEdited="" id="EditSubmitButton" name="EditUserSubmit" value="Yes">Submit</button>
                        <button class="btn btn-primary" style="margin:0 auto; display:none" asp-page-handler="SaveUserEdits" disabled id="SubmitButtonDisabled" name="AddUserSubmit" value="Yes">Submit</button>
                    </div>
                </div>
            </div>
        </div>

CS file: CS文件:

 public IActionResult OnPostDisplayEdit(int id, string email,string AttorneyRole,int pAttorneyRoleID,int pAttorney,int pEditAttorneyUserID,int PageFragment)
        {

            EditUserEmailInput = email;

            string curDictionaryAttorneyRole;

            if (AttorneyRole.Contains('('))//Just because in development user didnt always have ability to enter a party
            {

                curDictionaryAttorneyRole = AttorneyRole.Split(new[] { '(' }, 2)[0].Trim();

                EditUserPartyInput = AttorneyRole.Split(new[] { '(' }, 2)[1];

                var LastRightParenIndex = EditUserPartyInput.LastIndexOf(')');

                EditUserPartyInput = EditUserPartyInput.Substring(0, LastRightParenIndex); //Pulling the right parenthesis out
            }
            else //Its just the dictionary role
            {
                curDictionaryAttorneyRole = AttorneyRole;
                EditUserPartyInput = null;
            }

            TempData["EditingParty"] = "Yes";
            TempData["EndOfURL"] = PageFragment.ToString() + "&#" + PageFragment.ToString();

            SetFileSequenceField();
            ModelState.Clear(); //Used to remove the validations
            SetAttorneyRoleList();

            var selectedIndex = AttorneyRolesList.Where(arl => arl.AttorneyRole == curDictionaryAttorneyRole).Select(arl => arl.AttorneyRoleID).FirstOrDefault();

            AttorneyRoleIDEditForm = selectedIndex;
            EditAttorneyUserID = pEditAttorneyUserID;
            UneditedEmail = email;
            UneditedAttorneyRoleID = pAttorneyRoleID;
            PopulateUserList();
            //return RedirectToPage("AddUsersToCase","GET",fragment:PageFragment.ToString());
            return Page();

            //var PageURL = Page();

            //PageURL = PageURL. + "&#" + PageFragment.ToString();

            //return PageURL;
        }

You can try to replace你可以尝试更换

location.href = loadedURL + fragmentPieceOfURL;

to

history.pushState({}, null, loadedURL + fragmentPieceOfURL);

So that the page will not be refreshed with location.href .这样页面就不会被location.href刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM