我有一個註冊投票評論的方法。如果在投票時沒有錯誤,我通過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