七度&光 2016-09-14 20:49 采纳率: 22.2%
浏览 26

使用Ajax的MVC部分视图

I have made an MVC application and I'm trying to create a partial view for when the user clicks the "details" button. I have one partial view working at the moment for my "delete" button. When I step through my code using breakpoints it brings me as far as my Task Controller and steps into my PartialViewResult method but then goes no further. When I click the "details" button nothing happens. Not sure what is missing here.

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>

Task Controller

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);
}
  • 写回答

2条回答 默认 最新

  • weixin_33726313 2016-09-14 20:58
    关注

    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);
    }
    

    If you are expecting a list of Tasks try this:

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

    Or instead, you could edit _ShowTask model type to 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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)