gpt4 book ai didi

asp.net-mvc-4 - 具有动态部分 View 创建功能的 MVC Ajax

转载 作者:行者123 更新时间:2023-12-03 07:39:36 24 4
gpt4 key购买 nike

如何创建将调用动态部分 View 的动态 ajax.actionlinks。

例如:

  • 我的页面将生成 x 条评论
  • 每条评论都可以(单独)投赞成票或反对票
  • 赞成票数和反对票数均计为一个整数
  • 每个评论 div 都会有自己的 ajax.actionlink
  • 每个ajax.actionlink都会将评论的ID传递给 Controller ​​
  • Controller 将计算总票数并调用部分 View 以显示回具有正确 ID 的 div 中。

到目前为止我做了什么:

  • 我已经能够创建成功的 ajax.actionlink

  • 这将调用 Controller 并对投票进行求和

  • 这将调用部分 View 并显示投票

问题是什么

  • 我不想硬编码 30-100 个不同的 ajax.actionlinks 来调用 30-100 个硬编码的部分 View 。

如何动态地完成此操作?

现有代码:

我的 razor View 中的 ajax.actionlink

 @Html.Raw(Ajax.ActionLink("[replacetext]", "VoteUp",
new { UserPostID = @Model.Id },
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "CountVote" }).ToHtmlString().Replace("[replacetext]",
"<img src=\"/Images/up_32x32.png\" />"))

我的div位于同一个razor View 中,用于显示部分 View 的返回结果。

<div id="CountVote" class="postvotes"></div>

我的 Controller

    public PartialViewResult VoteUp(int UserPostID)
{
try
{
UserVotes vote = new UserVotes();
vote.SubmitedVote = 1;
vote.UserId = Convert.ToInt32(Session["id"]);
vote.UserPostID = UserPostID;
ViewBag.SumVotes = postRepository.InsertUserPostVote(vote);

}
catch (Exception e)
{
xxx.xxx.xxxx().Raise(e);
}
return PartialView("_TotalVotes");
}

最后是我的部分 View (_TotalVotes.cshtml)

@ViewBag.SumVotes

现在,我的 Viewpost 主视图使用 viewbag 循环显示评论。

foreach (var item in (List<UserComment>)ViewData["Comments"])
{
CommentVote = "cv" + i.ToString();
<div class="postlinewrapper">
<div class="postvotesframe">
<div class="postvotes">
@Html.Raw(Ajax.ActionLink("[replacetext]", "VoteUp",
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "CountVote" }).ToHtmlString().Replace("[replacetext]",
"<img src=\"/Images/up_32x32.png\" />"))
</div>

<div id="@CommentVote" class="@CommentVote">0</div>
<div class="postvotes">
@Html.Raw(Ajax.ActionLink("[replacetext]", "VoteDown",
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "CountVote" }).ToHtmlString().Replace("[replacetext]",
"<img src=\"/Images/down_32x32.png\" />"))
</div>
</div>
<div class="postleftbar">
@Html.Raw(item.Comment)
</div>
<div class="postrightbar">
<div>
<div class="post_spec">
<div class="post_spec_title">Call Sign: </div>
<div class="post_spec_detail">@item.CallSign</div>
</div>
<div class="post_spec">
<div class="post_spec_title">When: </div>
<div class="post_spec_detail">@item.CommentDate.ToString("dd/MM/yyyy")</div>
</div>
</div>
<br />
<br />
</div>
</div>
i += 1;
}

我已经实现了登录来上下增加或减少投票:

 public PartialViewResult VoteUp(int userPostId)
{
try
{
UserVotes vote = new UserVotes();
vote.SubmitedVote = 1;
vote.UserId = Convert.ToInt32(Session["id"]);
vote.UserPostID = userPostId;
ViewBag.SumVotes = postRepository.InsertUserPostVote(vote);

}
catch (Exception e)
{
xxxx.xxxx.xxxx().Raise(e);
}
return PartialView("_TotalVotes");
}

public PartialViewResult VoteDown(int userPostId)
{
try
{
UserVotes vote = new UserVotes();
vote.SubmitedVote = -1;
vote.UserId = Convert.ToInt32(Session["id"]);
vote.UserPostID = userPostId;
ViewBag.SumVotes = postRepository.InsertUserPostVote(vote);

}
catch (Exception e)
{
xxx.xxxx.xxxx().Raise(e);
}
return PartialView("_TotalVotes");
}

现在所有这些代码都适用于 1 个 ajax 调用,但我需要的是动态显示单独的 div 的单独的 ajax 调用。

最佳答案

试试这个方法。

主视图

我假设您有一个模型,其中包含评论项的集合属性Comments

@model MyNamespace.CommentAndOtherStuff

<ul>
@foreach(item in Model.Comments)
{
<li>
<a href="@Url.Action("VoteUp", "VoteControllerName", new { UserPostId = item.Id })"
class="vote-link"
data-id="@item.Id">@item.Votes</a><img src="vote.jpg" />
</li>
}
</ul>

您的 Controller 仅以 JSON 形式返回一个名为 VoteResult 的类。

[HttpPost]
public ActionResult VoteUp(int UserPostID)
{
...
var model = new VoteResult
{
UserPostID = UserPostID,
Votes = service.tallyVote(UserPostID)
};

return Json(model);
}

现在将所有这些与 jQuery 事件处理程序 Hook 并设置 AJAX 调用

$(document).ready(function() {

$("a.vote-link").on("click", function(event) {
event.preventDefault();
var link = $(this); // the link instance that was clicked
var id = link.attr("data-id");
var url = link.attr("href");

$.ajax({
url: url,
type: "post"
})
.done(function(result) {
// JSON result: { UserPostID: 1, Votes: 5 }

// replace link text
link.html(result.Votes);
});
});

});

但我想要一个部分 View html 片段。

[HttpPost]
public ActionResult VoteUp(int UserPostID)
{
...
var model = new VoteResult
{
UserPostID = UserPostID,
Votes = service.tallyVote(UserPostID)
};

return PartialView("_TotalVotes", model);
}

_总投票部分

@model MyNamespace.VoteResult

@if (Model.Votes < 0)
{
<span class="unpopular">@Model.Votes</span>
}
else
{
<span class="awesome">@Model.Votes</span>
}

并调整AJAX回调

.done(function(result) {
link.html(result);
});

现在您可以为链接片段编写一个帮助程序,但在我看来它混淆了事情(这是一个判断调用)。您真正需要的只是您的 javascript 将绑定(bind)的类名称和数据 ID。

关于asp.net-mvc-4 - 具有动态部分 View 创建功能的 MVC Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29583475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com