开发者

@Url.Action returns different results on subsequent calls

I am sure that this is something about jQuery/MVC3 that I just haven't figured out yet so if someone can tell me why this is the case.

When I call a action routine multiple times the @Url.Action routine returns different strings.

The following works no matter how many times it is called.

function DeletePhone( delId )
{
    var va = $("#idPhoneNumberView *").开发者_运维知识库serializeArray();
    $.ajax({
        url: '@Url.Action( "DeletePhone", "Player")/' + delId,
        data: JSON.stringify(va),
        type: 'POST',
        contentType: 'application/json',
        dataType: 'html',
        success: function (resp)
        {
            var ev = document.getElementById("idPhoneNumberView");
            ev.innerHTML = resp;
        },
        error: function (x)
        {
            alert(x.status);
        }
    });
};

This routine only works the first time.

function DeletePhone( delId )
{
    var va = $("#idPhoneNumberView *").serializeArray();
    $.ajax({
        url: '@Url.Action( "DeletePhone", "Player")/' + delId,
        data: JSON.stringify(va),
        type: 'POST',
        contentType: 'application/json',
        dataType: 'html',
        success: function (resp)
        {
            $("#idPhoneNumberView").html(resp);
        },
        error: function (x)
        {
            alert(x.status);
        }
    });
};

The url is "/Mvc/Player/DeletePhone/0" on the first call and it is "/Mvc/Player/DeletePhone/0/0" on the subsequent calls, since I get a 404 after the first call which is displayed from the error function, which is understandable given the url.

The processing is different when I use the jQuery $("#idPhoneNumberView").html(resp), so if someone could get me up to speed it would be appreciated.

I want to be sure I am clear on this. The ONLY difference in the code the works and the one that doesn't is the use of jQuery as opposed to using getElementById directly in the success function.

I do not understand how the use of the jQuery html method causes the @Url.action method to create different paths on subsequent calls. Scott


Url.Action is partially based on the current url used to generate the request. If the request is / then that's all the resulting url will be because it's the same page that you're on. If you're going to a different controller or action then it will generate a url with only the values you specify.

For instance: going to /home/action/1 having a Url.Action("action", "home") on that view the resulting path will be the same page and will thus just use the url passed rather than generate a new url. (Strange behavior to me now that I see it)

Unfortunately, the only solution I know is to do the following

@Url.Action("DeletePhone", "Player", new { id = UrlParameter.Optional })

(assuming you're using the default route where {id} is the parameter name

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜