使用Jquery将另一个列表替换为一个列表

时间:2016-06-02 15:14:56

标签: jquery asp.net-mvc

我在名为Model的视图中有一个列表,我想使用jquery在选中该框时替换为新列表。最简单的方法是什么?

$(function () {
    $('input[type=checkbox]').change(function () {
        if (this.checked) {
            $.post("/Authentication/GetPeopleThatHaveOwnTransportation", {}, function (PeopleList) {
                //What do I write to replace this list (PeopleList) with the Model
            });
        }
    });
});
<div class="row">
    <div class="list">
    @foreach (var c in Model)
    {
        <div class="col-sm-4 col-lg-4 col-md-4">
            <div class="card hovercard">
                <div class="cardheader"></div>
                <div class="avatar">
                    <img alt="" src="/Uploads/@c.Picture">
                </div>
                <div class="info">
                    <div class="title">
                        <a target="_blank" href="http://localhost:53008/authentication/profileperson?id=@c.id">@c.FirstName @c.LastName</a>
                    </div>
                    <div class="desc">Age: @c.Age</div>
                    <div class="desc">Date Joined: @c.DateJoined</div>
                </div>
            </div>
        </div>
    }
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

你应该写这样的东西:

$('.list').empty();
$('.list').html(PeopleList);

要使其工作,您需要将模型发送到局部视图,渲染它,并将渲染的字符串作为json返回。

此问题和答案可能会引导您走上正确的道路: Render Partial View Using jQuery in ASP.NET MVC

答案 1 :(得分:0)

以下内容应覆盖&#34; .list&#34;的内容。 div(由加载模型填充)和AJAX查询的返回结果:

$(function () {
    $('input[type=checkbox]').change(
     function () {
         if (this.checked) {
             $.post("/Authentication/GetPeopleThatHaveOwnTransportation", {}, function (PeopleList) {
                $(".list").html(PeopleList);
             });
         }
     });
});

而不是在你的&#34; GetPeopleThatHaveOwnTransportation&#34;中明确地传回模型。动作,你可以传回一串创建列表的HTML,就像你在剃刀中看到的一样。