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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

Change 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