English 中文(简体)
改用伙伴关系的另一页。 NET MVC
原标题:Redirecting to another page in ASP.NET MVC using JavaScript/jQuery

我想从一页转至文部的另一页。 NET MVC 3.0 , 采用 Java/jQuery/Ajax。 在纽顿点击事件上,我写了 Java版,如下文所示。

function foo(id)
{
    $.post( /Branch/Details/  + id);
}

我的管制人员守则就是这样:

public ViewResult Details(Guid id)
{
     Branch branch = db.Branches.Single(b => b.Id == id);
     return View(branch);
}

当我点击一个 but子时,它叫作主计长办公室内的详细行动,但它没有回去详细的看法。

我没有发现任何错误或例外。 http://en.wikipedia.org/wiki/Firebug_%28software%29” 我的法典中有什么错误,我如何改用详细的观点网页?

最佳回答

您在后美加澳新军呼吁中并未提及任何成功呼吁。 要求得到执行,但与结果无关。 如果你想对结果做一些有益的事,就会尝试:

$.post( /Branch/Details/  + id, function(result) {
    // Do something with the result like for example inject it into
    // some placeholder and update the DOM.
    // This obviously assumes that your controller action returns
    // a partial view otherwise you will break your markup
});

On the other hand if you want to redirect, you absolutely do not need AJAX. You use AJAX only when you want to stay on the same page and update only a portion of it.

So if you only wanted to redirect the browser:

function foo(id) {
    window.location.href =  /Branch/Details/  + id;
}

As a side note: You should never be hardcoding urls like this. You should always be using url helpers when dealing with urls in an ASP.NET MVC application. So:

function foo(id) {
    var url =  @Url.Action("Details", "Branch", new { id = "__id__" }) ;
    window.location.href = url.replace( __id__ , id);
}
问题回答

可以通过在观点中使用隐蔽的变量来做到这一点,然后将这一变量从 Java成文法中删除。

这里,我认为我的法典是我的。

@Html.Hidden("RedirectTo", Url.Action("ActionName", "ControllerName"));

现在,你可以在贾瓦伦文文本中使用:

 var url = $("#RedirectTo").val();
 location.href = url;

它与一家药店一样工作。 我希望这也有助于你。

您可以使用:

window.location.href =  /Branch/Details/  + id;

But your Ajax code is incomplete without success or error functions.

// in the HTML code I used some razor
@Html.Hidden("RedirectTo", Url.Action("Action", "Controller"));

// now down in the script I do this
<script type="text/javascript">

var url = $("#RedirectTo").val();

$(document).ready(function () {
    $.ajax({
        dataType:  json ,
        type:  POST ,
        url:  /Controller/Action ,
        success: function (result) {
            if (result.UserFriendlyErrMsg ===  Some Message ) {
                // display a prompt
                alert("Message: " + result.UserFriendlyErrMsg);
                // redirect us to the new page
                location.href = url;
            }
            $( #friendlyMsg ).html(result.UserFriendlyErrMsg);
        }
    });
</script>
<script type="text/javascript">
    function lnkLogout_Confirm()
    {
        var bResponse = confirm( Are you sure you want to exit? );

        if (bResponse === true) {
            ////console.log("lnkLogout_Confirm clciked.");
            var url =  @Url.Action("Login", "Login") ;
            window.location.href = url;
        }
        return bResponse;
    }

</script>

检查以下守则将有助于:

<script type="text/javascript">
  window.opener.location.href =  @Url.Action("Action", "EventstController") , window.close();
</script>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签