admin管理员组文章数量:1323723
I have a method which registers a vote for a ment. If there are no errors while casting the vote, I return a small snippet of html via a PartialViewResult to update the page.
If it does not succeed, nothing should happen. I need to test for this condition on the client side.
The server-side method:
[HttpPost]
public PartialViewResult RegisterVote(int mentID, VoteType voteType) {
if (User.Identity.IsAuthenticated) {
var userVote = repository.RegisterVote((Guid)Membership.GetUser().ProviderUserKey, mentID, voteType);
if (userVote != null) {
return PartialView("VoteButtons", userCommentVote.Comment);
}
}
return null;
}
The client side script:
$(document).on("click", ".vote img", function () {
var image = $(this);
var mentID = GetCommentID(image);
var voteType = image.data("type");
$.post("/TheSite/RegisterVote", { mentID: mentID, voteType: voteType }, function (html) {
image.parent().replaceWith(html);
});
});
If the vote was recorded, the "html" variable containes markup as expected. If it does not succeed (i.e. null was returned) then the "html" variable is instead a "Document" object with a parse error.
Is there a way to return an empty string from the PartialViewResult and then just test for length? Is there a different/better way to do this?
I have a method which registers a vote for a ment. If there are no errors while casting the vote, I return a small snippet of html via a PartialViewResult to update the page.
If it does not succeed, nothing should happen. I need to test for this condition on the client side.
The server-side method:
[HttpPost]
public PartialViewResult RegisterVote(int mentID, VoteType voteType) {
if (User.Identity.IsAuthenticated) {
var userVote = repository.RegisterVote((Guid)Membership.GetUser().ProviderUserKey, mentID, voteType);
if (userVote != null) {
return PartialView("VoteButtons", userCommentVote.Comment);
}
}
return null;
}
The client side script:
$(document).on("click", ".vote img", function () {
var image = $(this);
var mentID = GetCommentID(image);
var voteType = image.data("type");
$.post("/TheSite/RegisterVote", { mentID: mentID, voteType: voteType }, function (html) {
image.parent().replaceWith(html);
});
});
If the vote was recorded, the "html" variable containes markup as expected. If it does not succeed (i.e. null was returned) then the "html" variable is instead a "Document" object with a parse error.
Is there a way to return an empty string from the PartialViewResult and then just test for length? Is there a different/better way to do this?
Share Improve this question asked Mar 9, 2012 at 1:54 JasonJason 8,65011 gold badges58 silver badges70 bronze badges2 Answers
Reset to default 5Change your method signature from: public PartialViewResult
To: public ActionResult
Then instead of returning null, return this:
return Json("");
This will allow you to return a partial view if successful, if not, it will just return JSON with an empty string as the value. Your current JS will work as is. From MSDN:
The ActionResult class is the base class for action results.
The following types derive from ActionResult:
- ContentResult
- EmptyResult
- FileResult
- HttpUnauthorizedResult
- JavaScriptResult
- JsonResult
- RedirectResult
- RedirectToRouteResult
- ViewResultBase
This is what allows you to return different derived types in your method.
It would be better to return a JsonResult as,
[HttpPost]
public JsonResult RegisterVote(int mentID, VoteType voteType)
{
JsonResult result = new JsonResult();
object content;
if (User.Identity.IsAuthenticated)
{
var userVote = repository.RegisterVote((Guid)Membership.GetUser().ProviderUserKey, mentID, voteType);
if (userVote != null)
{
content = new
{
IsSuccess = true,
VoteButtons = userCommentVote.Comment
};
}
else
{
content = new { IsSuccess = false };
}
}
result.Data = content;
return result;
}
In Ajax call, you can validate if IsSuccess
is true
or false
.
本文标签: javascriptHow can I return an empty string (or null) for a PartialViewResultStack Overflow
版权声明:本文标题:javascript - How can I return an empty string (or null) for a PartialViewResult? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122232a2421768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论