简体   繁体   中英

MVC3 Action Call from Javascript ReturnUrl parameter always null

I am using javascript to load up an Action and finding that when the action method is called one of the parameters "returnUrl" is always null. I have confirmed that returnUrl is populated in the javascript correctly using firebug, but somewhere between executing the .load function and the action method the value for returnUrl is lost and set to null. I have found that if I remove the "id" parameter and just have the "returnUrl" parameter that returnUrl has the correct value. I have spent many hours trying to figure out what is going on here and am completely stumped, I would apprectiate some help.

My Javascript:

<!-- Review Dialog Popup -->
<script type="text/javascript">
function showWriteReviewDialog(gameId, returnUrl) {
    if( $("#Review").length == 0)
    { 
        var url = "@Url.Action("WriteUserReview", "UGDBUser", new { id = "PLACEHOLDER", returnUrl =  Request.Url.ToString() })";

        // ajax load
        $('#writereview').load(url.replace('PLACEHOLDER', gameId));
    } else {
        // clear summary & reviewtext fields
        $('#summary,#reviewtext').val('');

        //reopen the write review dialog which was previously rendered
        $("#Review").dialog({
            modal: true, 
            autoOpen: true,
            resizeable: false
        }); 
    }
};
</script>

My Dumbed Down Action Method:

     [Authorize]
     public ActionResult WriteUserReview(Guid id, string returnUrl)
     {
        return Redirect(returnUrl);
     }

There must be something wrong with the URL generated. Also make sure the id is a guid. Here is an example.

Option 1

function showWriteReviewDialog(gameId, returnUrl) {

    var url = '@Url.Action("TestParams", "Home")?id=' + gameId + '&returnUrl=' + returnUrl;
    $("#writereview").load(url);

    //rest of your operations
};

Option 2

function showWriteReviewDialog(gameId, returnUrl) {

    var url = '@Url.Action("TestParams", "Home", new { id = "guid_replace", returnUrl = "url_replace"})';
    url = url.replace('guid_replace', gameId);
    url = url.replace('url_replace', returnUrl);

    $("#writereview").load(url);

    //rest of your operations
};

Screen shot on hitting the action; it returns both the values (have a look at the watch window)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM