使用ajax的MVC部分视图

本文关键字:视图 MVC ajax 使用 | 更新日期: 2023-09-27 18:07:05

我做了一个MVC应用程序,我试图创建一个部分视图,当用户点击"详情"按钮。我有一个部分视图工作在我的"删除"按钮的时刻。当我使用断点步进我的代码时,它会带我到我的任务控制器,并步进我的PartialViewResult方法,但随后就没有进一步了。当我点击"细节"按钮时,什么也没有发生。不知道这里缺少了什么。

Index.cshtml

       <span class="btn btn-success btn-sm" onclick="showTask('@item.TaskId')">Details</span>
<div id="Detail"></div>
<Script>
        function showTask(showTaskID) {
        $.ajax({
            url: '@Url.Action("ShowTaskByID")',
            data: { id: showTaskID },
            success: function (data) {
                $('#Detail').hide();
                $('#Detail').html(data);
                $('#Detail').animate({
                    opacity: 1,
                    left: "+=50",
                    height: "toggle"
                }, 3000, function () {
                    // Animation complete.
                });
                $('#Edit').hide();
                $('#Delete').hide();
            },
            error: function (data) { $('#Details').html('<h3>Error</h3>'); }
        });
    }

</script>

_ShowTask

@model IEnumerable<WebApplication4.Models.Task>
<div class="panel panel-info">
    <div class="panel-heading" style="font-size:20px">
        <h2>List of Actors</h2>
    </div>
    <p>
        @Html.ActionLink("Create New Task", "CreateTask", null, new { @class = "btn btn-sm btn-primary" })
    </p>
    @if (Model.Any())
    {
        <table class="table table-condensed table-striped">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.First().TaskName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.First().StartDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.First().FinishDate)
                </th>
                <th></th>
            </tr>
            @if (Model != null)
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.TaskName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.StartDate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.FinishDate)
                        </td>
                        <td>
                            @Html.ActionLink("Edit", "EditTask", new { id = item.TaskId }, new { @class = "btn btn-info btn-xs" })
                            @*<a onclick="showEditActor('@item.MovieID','@item.ActorID','@item.ActorName','@item.age')" class="btn btn-xs btn-info">Edit</a>*@
                            @Html.ActionLink("Delete", "DeleteTask", new { id = item.TaskId }, new { @class = "btn btn-danger btn-xs" })
                        </td>
                    </tr>
                }
            } @* closing of if *@
        </table>
    }
    else
    {
        <div><strong>No Actors in Movie</strong></div>
    }
</div>
任务控制器

public ActionResult Details(int id)
{
    var q = db.Tasks.Find(id);
    if (q == null)
    {
    }
    return View(q);
}
public PartialViewResult ShowTaskByID(int id)
{
    return PartialView("_ShowTask", db.Tasks.Find(id).TaskName);
}

使用ajax的MVC部分视图

HTML

<input id="btnDetail" type="button" class="btn btn-success btn-sm" value="Details" />
<div id="Detail"></div>

JS

$('#btnDetail').on('click', function(){
    $.ajax({ 
        url: '@Url.Action("ShowTaskByID")',
        data: { id: showTaskID },
    }).done(function (data) {
        $('#Detail').html(data);
        $('#Detail').animate({
            opacity: 1,
            left: "+=50",
            height: "toggle"
        }, 3000, function () {
            // Animation complete.
        });
        $('#Edit').hide();
        $('#Delete').hide();
    }).fail(function (jqXHR, textStatus) {
        $('#Detail').html('<h3>Error :' + jqXHR.responseText + '</h3>'); 
    });
});
c#

public ActionResult Details(int id)
{
    try 
    {
       var task = db.Tasks.Find(id);
    }
    catch(HttpException e) 
    {
       throw new HttpException(404, "Task not found.")
    }
    return View(task);
}
public PartialViewResult ShowTaskByID(int id)
{
    try
    {
        var tasks = db.Tasks.Find(id).TaskName;    
    }
    catch(HttpException e) 
    {
       throw new HttpException(404, "Task nout found.")
    }
    return PartialView("_ShowTask", tasks);
}

如果你想要一个任务列表,试试这个:

public PartialViewResult ShowTaskByID()
{
    try
    {
        var tasks = db.Tasks.ToList();
    }
    catch(HttpException e) 
    {
       throw new HttpException(404, "Task nout found.")
    }
    return PartialView("_ShowTask", tasks);
}

或者,您可以将_ShowTask模型类型编辑为Task:

@model WebApplication4.Models.Task
<div class="panel panel-info">
    <div class="panel-heading" style="font-size:20px">
        <h2>List of Actors</h2>
    </div>
    <p>
        @Html.ActionLink("Create New Task", "CreateTask", null, new { @class = "btn btn-sm btn-primary" })
    </p>
    @if (Model.Any())
    {
        <table class="table table-condensed table-striped">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.TaskName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.StartDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.FinishDate)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        @Html.DisplayFor(model => model.TaskName)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model.StartDate)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model.FinishDate)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "EditTask", new { id = Model.TaskId }, new { @class = "btn btn-info btn-xs" })
                        @*<a onclick="showEditActor('@item.MovieID','@item.ActorID','@item.ActorName','@item.age')" class="btn btn-xs btn-info">Edit</a>*@
                        @Html.ActionLink("Delete", "DeleteTask", new { id = Model.TaskId }, new { @class = "btn btn-danger btn-xs" })
                    </td>
                </tr>
            </tbody>    
            } @* closing of if *@
        </table>
    }
    else
    {
        <div><strong>No Actors in Movie</strong></div>
    }
</div>

[HttpGet]添加标签到您的public PartialViewResult ShowTaskByID(int id)

所以,结果应该是:

[HttpGet] public PartialViewResult ShowTaskByID(int id)