Jason
Jason

Reputation: 8640

How can I return an empty string (or null) for a PartialViewResult?

I have a method which registers a vote for a comment. 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 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;
}

The client side script:

$(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);
    });
});

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?

Upvotes: 5

Views: 7320

Answers (2)

Manas
Manas

Reputation: 2542

It would be better to return a JsonResult as,

    [HttpPost]
    public JsonResult RegisterVote(int commentID, VoteType voteType)
    {
        JsonResult result = new JsonResult();
        object content;
        if (User.Identity.IsAuthenticated)
        {
            var userVote = repository.RegisterVote((Guid)Membership.GetUser().ProviderUserKey, commentID, 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.

Upvotes: 0

Paul
Paul

Reputation: 12440

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.

Upvotes: 5

Related Questions