【问题标题】:Razor Communicate Display of Modal from PageModel's OnPost() MethodRazor 从 PageModel 的 OnPost() 方法中传达 Modal 的显示
【发布时间】:2021-10-09 16:50:32
【问题描述】:

我想通过我的 Razor PageModel 的 OnPost() 方法进行通信,以在验证错误时显示模式。这基本上意味着将模态的 css 从 display none 更改为 block。有没有办法做到这一点?

目前在返回 Page() 时,模式被隐藏,因为这是它的 css 最初设置的内容,并且通常在用户单击按钮以显示它时显示。我在我的 PageModel 代码中标记了我希望发生通信的位置

@page
@{
    ViewData["Folder"] = "CourtUser";
    <form asp-action="AddorEditUsersOnHearing" method="post" name="AddorEditUsersOnHearingForm" id="AddorEditUsersOnHearing">

        <div class="container">
            <div class="row">
                <div class="col-md-8 mb-4">
                    <p>Add/Edit Users on  <b style="font-style:italic">@Model.HearingName </b></p>                   
                    <div class="modal" tabindex="-1" role="dialog" id="AddUserForm">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">Add User</h5>
                                    <button type="button" onclick="closeAddUserForm()" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="form-group" style="margin-top:5px;padding-left:45px">
                                    <label asp-for="AddUserInput" style="width:100px"></label>
                                    <input asp-for="AddUserInput" class="form-control col-4" id="EmailInputBox" style="display:inline-block" onchange="ValidateEmail()" />
                                    <span style="display:block" asp-validation-for="AddUserInput" class="text-danger"></span>

                                </div>
                                <div class="modal-footer">
                                    <button class="btn btn-primary" style="margin:0 auto" asp-page-handler="AddUser" name="AddUserSubmit" value="Yes">Submit</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <input asp-for="HearingId" type="hidden" />
                    <input asp-for="HearingName" type="hidden" />

                    <button type="button" class="btn btn-primary" onclick="ShowAddUserForm()">Add User</button>
                    <button style="float:right" class="btn btn-primary">Remove User(s)</button>


                </div>
            </div>


        </div>
    </form>

}

<script type="text/javascript">
    function ShowAddUserForm() {
        document.getElementById("AddUserForm").style.display = "block";
    }
    function closeAddUserForm() {
        document.getElementById("AddUserForm").style.display = "none";
    }
</script>
        public IActionResult OnPostAddUser()
        {
            if (ModelState.IsValid)
            {
                if (AddUserInput == null)
                {
                   
                    ModelState.AddModelError("AddUserInput", "Please enter an email");
                     
                    UsersonHearingList = HttpContext.Session.GetObjectFromJson<List<UsersModel>>("UsersonHearingList");
                     
//*****This is where I want to communicate to the view to display the modal.*******
                    return Page();
                }

               
            }
            else
            {
                return RedirectToPage("/Shared/Error");
            }
        }

【问题讨论】:

    标签: javascript asp.net-core razor


    【解决方案1】:

    您可以尝试使用TempData。这是一个演示:

    js:

    @section Scripts
    {
        <script type="text/javascript">
        $(function () {
            if ("@TempData["Modal"]" == "Display")
            {
                ShowAddUserForm();
            }
        });
        function ShowAddUserForm() {
            document.getElementById("AddUserForm").style.display = "block";
        }
        function closeAddUserForm() {
            document.getElementById("AddUserForm").style.display = "none";
        }
        </script>
    }
    

    处理程序:

    public IActionResult OnPostAddUser()
            {
                if (ModelState.IsValid)
                {
                    if (AddUserInput == null)
                    {
    
                        ModelState.AddModelError("AddUserInput", "Please enter an email");
    
                        UsersonHearingList = HttpContext.Session.GetObjectFromJson<List<UsersModel>>("UsersonHearingList");
    
                        //*****This is where I want to communicate to the view to display the modal.*******
                        TempData["Modal"] = "Display";
                        return Page();
                    }
    
    
                }
                else
                {
                    return RedirectToPage("/Shared/Error");
                }
            }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2021-10-25
      • 1970-01-01
      • 2018-11-07
      • 2022-07-27
      • 2021-06-05
      • 2020-07-09
      • 2020-10-15
      相关资源
      最近更新 更多