如果View中有多个div副本,如何使用“ Ajax.ActionLink”仅刷新一个特定的div

时间:2018-12-26 14:21:06

标签: c# asp.net asp.net-mvc asp.net-ajax

我正在为我的大学项目建立一个网站,但是在使用Ajax.ActionLink刷新View时遇到了问题。我创建了一个主页,有点像“ 9gag”,其中许多图片相互存储,并且所有图片都在同一div上。当我尝试对div进行投票时,它将在后端很好地工作,但是将始终仅更新element的第一个副本,而不是我想要的那个。

查看:

<ContentControl Grid.Column="1"
                Margin="7,0,7,0">
    <ContentControl.Resources>
        <mapping:Slide11x4MappingView x:Key="Slide11X4MappingView" DataContext="{Binding MappingViewModel}"/>
        <mapping:MicrotubeMappingView x:Key="MicrotubeMappingView"  DataContext="{Binding MappingViewModel}"/>
    </ContentControl.Resources>
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Acquirer.Sorter.TrayType}" Value="{x:Static mapping:TrayType.SLIDES11X4}">
                    <Setter Property="Content" Value="{StaticResource Slide11X4MappingView}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Acquirer.Sorter.TrayType}" Value="{x:Static mapping:TrayType.VIALS}">
                    <Setter Property="Content" Value="{StaticResource MicrotubeMappingView}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>

控制器:

    @foreach (var item in Model.gagList)
    {
        <div class="gag-object">
            <span class="gag-title">@Html.DisplayFor(modelItem => item.Title)</span><br />
            <img class="gag-picture" src="~/UserGagImages/@Html.DisplayFor(modelItem => item.Picture)" style="width:460px"> <br />

            <a href=@Url.Action("IndexCategory", "Home", new { CategoryName = item.Category.CategoryName }) style="text-decoration: none;" class="action-text"> <span class="gag-span">@Html.DisplayFor(modelItem => item.Category.CategoryName)</span><br /></a>
            <div class="gag-points" id="gag-points">
                <span class="gag-title">@Html.DisplayFor(modelItem => item.Points)</span><br />
            </div>
            <div class="gag-vote">
                <a style="color: deepskyblue; font-size: 20px;" class="fas fa-arrow-up" href="@Url.Action("Upvote","Home", new { id = item.GagId })"></a>
                <a style="color: deepskyblue; font-size: 20px;" class="fas fa-arrow-down"></a>
                @Ajax.ActionLink("Upvote",
                  "Upvote",
                  new { id = item.GagId },
                  new AjaxOptions
                  {
                      HttpMethod = "GET",
                      UpdateTargetId = "gag-points",
                      InsertionMode = InsertionMode.Replace
                  },
                  new { @class = "ajax-btn3", id = "myajaxLink", gagId = item.GagId })

            </div>

        </div>
        <hr />

    }

部分视图:

        public ActionResult Upvote(int id)
    {
        Gag gagUpvote = new Gag();
        if (Request.IsAuthenticated)
        {
            using (IGagsEntities1 context = new IGagsEntities1())
            {
                gagUpvote = context.Gags.Where(a => a.GagId == id).FirstOrDefault();
                gagUpvote.Points += 1;
                context.SaveChanges();
            }
        }
        return PartialView("Points", gagUpvote);
    }

通过这种组合,它不会更新我想要的div,但始终会更新第一个副本。如果您对如何设置它有任何想法,它将使div成为我想要的任何提示,我将不胜感激!

0 个答案:

没有答案