我有一个注册投票评论的方法。如果在投票时没有错误,我通过PartialViewResult返回一小段html来更新页面。如何为PartialViewResult返回空字符串(或null)?
如果不成功,则不会发生任何事情。我需要在客户端测试这种情况。
服务器端方法:
[HttpPost]
public PartialViewResult RegisterVote(int commentID, VoteType voteType) {
if (User.Identity.IsAuthenticated) {
var userVote = repository.RegisterVote((Guid)Membership.GetUser().ProviderUserKey, commentID, voteType);
if (userVote != null) {
return PartialView("VoteButtons", userCommentVote.Comment);
}
}
return null;
}
客户端脚本:
$(document).on("click", ".vote img", function() {
var image = $(this);
var commentID = GetCommentID(image);
var voteType = image.data("type");
$.post("/TheSite/RegisterVote", { commentID: commentID, voteType: voteType }, function (html) {
image.parent().replaceWith(html);
});
});
如果投票记录,在 “HTML” 变量containes标记预期。如果它不成功(即返回null),那么“html”变量是一个带有分析错误的“Document”对象。
有没有办法从PartialViewResult返回空字符串,然后只是测试长度?有没有不同的/更好的方法来做到这一点?
工作就像一个魅力。谢谢 – Jason 2012-03-09 04:10:01