简体   繁体   English

为什么我的 asp.net 核心 cshtml 视图不呈现?

[英]Why is my asp.net core cshtml view not rendering?

When I press the delete button in my DataTable it should bring me to the DeleteUserConfirm view.当我按下 DataTable 中的删除按钮时,它应该将我带到 DeleteUserConfirm 视图。 I can see that the data needed to display the page is being passed.我可以看到正在传递显示页面所需的数据。 However, the page is not displaying and I do not receive any errors.但是,该页面未显示,我没有收到任何错误。 Any thoughts or suggestions to fix this would be greatly appreciated!任何解决此问题的想法或建议将不胜感激!

The code for the DataTable is below:数据表的代码如下:

<html>
<head>
    <!--CSS for DataTables-->
    <link href="//cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css" rel="stylesheet" />
</head>
<body>
    <h1>All Accounts</h1>
    <br />
    <br />
    <div>
        <table id="allAccounts" class="table table-striped table-bordered" style="width:100%">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Organization</th>
                    <th>State</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div>
        <a asp-action="Index">Back to Home</a>
    </div>
</body>
</html>


@section Scripts{
    <script type="text/javascript" src="~/lib/jquery/dist/jquery.js"></script>
    <script type="text/javascript" src="//cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#allAccounts').DataTable(
                {
                    "responsive": true,
                    "ajax": {
                        "url": "/api/User/GetUsers",
                        "dataSrc": ""

                    },
                    "columns": [
                        { "data": "FirstName" },
                        { "data": "LastName" },
                        { "data": "Organization" },
                        { "data": "State" },
                        {
                            "data": "Id",
                            "render": function (data, type, row, meta) {
                                return "<button class='btn btn-danger' style=margin-right:5px; onclick=DeleteUser(" + row.Id + ")>Delete</button>"

                            }
                        }
                    ]
                });
        });

        function DeleteUser(data) {
            $.ajax({
                "type": "POST",
                "url": "/api/User/Delete/",
                "data": JSON.stringify(data),
                "contentType": "application/json; charset=utf-8"
            })
        }
    </script>
}

Once the delete button is clicked, the data is sent to the Delete method in the User Controller, which is an API Controller.单击删除按钮后,数据将发送到用户 Controller 中的 Delete 方法,即 API Controller。 The code for the Delete method is below: Delete 方法的代码如下:

[HttpPost]
    public IActionResult Delete([FromBody] int data)
    {
       

        return RedirectToAction("DeleteUserConfirm", "Home", new { id = data });
    }

The Delete method in the User Controller redirects to the Home Controller where the data is passed and the DeleteUserConfirm view should be displayed. User Controller 中的 Delete 方法重定向到 Home Controller ,在此传递数据并应显示 DeleteUserConfirm 视图。 The DeleteUserConfirm method from the Home Controller is below: Home Controller 中的 DeleteUserConfirm 方法如下:

 [HttpGet]
        public IActionResult DeleteUserConfirm(int id)
        {              
            return View(_userRepository.GetUser(id));
        }

The code for the DeleteUserConfirm view is below: DeleteUserConfirm 视图的代码如下:

@model Project.Models.User

@{
    ViewData["Title"] = "Confirm Rejection/Deletion";
}

<h1>Confirm Account Rejection/Deletion</h1>
<br/>
<br/>
<h4>Are you sure you want to reject and delete this account?</h4>
<div>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.FirstName)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.LastName)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.LastName)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EmailAddress)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EmailAddress)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.PhoneNumber)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.PhoneNumber)
        </dd>
    </dl>

    <form asp-action="DeleteUser">
        <input type="hidden" name="id" value="@Model.Id" />
        <input type="submit" value="Delete" name="submit" class="btn btn-danger" /> |
        <input type="submit" value="Cancel" name="submit" class="btn btn-primary" formnovalidate />
    </form>
    <br/>
    <a asp-action="Accounts">Back to Account Management</a>
</div>

I make it all the way to the DeleteUserConfirm method in the Home Controller.我一直到 Home Controller 中的 DeleteUserConfirm 方法。 I can see that the user is being retrieved from the GetUser(Id) method.我可以看到正在从 GetUser(Id) 方法中检索用户。 I just do not get taken to the view itself.我只是不被带到视图本身。

You won't be able to RedirectToAction in the controller because your request is coming from client side via ajax.您将无法在 controller 中进行 RedirectToAction,因为您的请求来自客户端通过 ajax。

Instead, just redirect directly to your DeleteUserConfirm when the user clicks the button.相反,只需在用户单击按钮时直接重定向到您的 DeleteUserConfirm。

        $(document).ready(function () {
            $('#allAccounts').DataTable(
                {
                    "responsive": true,
                    "ajax": {
                        "url": "/api/User/GetUsers",
                        "dataSrc": ""

                    },
                    "columns": [
                        { "data": "FirstName" },
                        { "data": "LastName" },
                        { "data": "Organization" },
                        { "data": "State" },
                        {
                            "data": "Id",
                            "render": function (data, type, row, meta) {
                                return "<a class='btn btn-danger' style='margin-right:5px;' href='/Home/DeleteUserConfirm/" + row.Id + "'>Delete</a>"
                            }
                        }
                    ]
                });
        });

}

You cannot do it this way using ajax .您不能使用ajax这样做。 Returning a view is possible, but you need to update it manually via javascript.可以返回视图,但您需要通过 javascript 手动更新它。 Currently your ajax is not doing anything after successful request.目前,您的 ajax 在成功请求后没有做任何事情。 Update your ajax to this:将您的 ajax 更新为:

$.ajax({
   "type": "POST",
   "url": "/api/User/Delete/",
   "data": JSON.stringify(data),
   "contentType": "application/json; charset=utf-8"
}).done(function(html){
    $('#Container').html(html);
}).fail(function(){
   console.error('error');
});

and ideally, you should return PartialView from controller.理想情况下,您应该从 controller 返回PartialView

[HttpGet]
public IActionResult DeleteUserConfirm(int id)
{              
    return PartialView(_userRepository.GetUser(id));
}

You may have to call home/DeleteUserConfirm/ directly in ajax, but you can try with API to see if its working.您可能必须直接在 ajax 中调用home/DeleteUserConfirm/ ,但您可以尝试使用 API 来查看它是否工作。

or if you just need to redirect to another page, do it in done callback.或者如果您只需要重定向到另一个页面,请在done回调中进行。

$.ajax({
   "type": "POST",
   "url": "/api/User/Delete/",
   "data": JSON.stringify(data),
   "contentType": "application/json; charset=utf-8"
}).done(function(){
    window.location.href = 'url';
}).fail(function(){
   console.error('error');
});

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

相关问题 Asp.NET 内核,发布时找不到cshtml的视图 - Asp.NET Core, unable to find View of cshtml when published 在 Layout 中渲染局部视图并在 asp.net core 中为其声明一个控制器 - Rendering a partial view in Layout and declare a controller for it in asp.net core 在 cshtml 中重用编辑和新建视图内容的 ASP.NET Core 特定方法 - ASP.NET Core specific way to reuse Edit and New view content in cshtml 如何在ASP.Net Core2 cshtml文件中访问数据库(查看) - How to access database in ASP.Net Core2 cshtml file (View) 如何使用 ASP.NET Core MVC 识别您来自的 view.cshtml? - How to recognize the view.cshtml that you are coming from using ASP.NET Core MVC? 为什么我的 AJAX 表单从我在 ASP.NET Core 的部分视图中发布多次? - Why is my AJAX form posting mutliple times from my partial view in ASP.NET Core? Asp.Net Core PageStart.cshtml的替代方案? - Asp.Net Core PageStart.cshtml alternative? 运行时编辑cshtml时,asp.net Core崩溃 - Asp.net Core crashes when editing cshtml while running 如何重定向到 asp.net 核心中的另一个“.cshtml”文件? - How to redirect to another ".cshtml" file in asp.net core? 在ASP.NET Core中的_Layout.cshtml中访问cookie - access cookie in _Layout.cshtml in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM