ASP의 다른 페이지로 리디렉션합니다.자바스크립트/jQuery를 이용한 NET MVC
ASP의 한 페이지에서 다른 페이지로 리디렉션합니다.자바스크립트/jQuery/Ajax를 사용하는 NET MVC 3.0버튼 클릭 이벤트 아래와 같이 자바스크립트 코드를 작성하였습니다.
function foo(id)
{
$.post('/Branch/Details/' + id);
}
내 컨트롤러 코드는 다음과 같습니다.
public ViewResult Details(Guid id)
{
Branch branch = db.Branches.Single(b => b.Id == id);
return View(branch);
}
버튼을 클릭하면 BranchController 내부의 Details 작업이 호출되지만 Details 보기로 돌아가지 않습니다.
저는 오류나 예외는 없었습니다.파이어버그에서 상태 200 OK를 표시하고 있습니다.내 코드에 문제가 있는 사항 및 세부 정보 보기 페이지로 리디렉션하려면 어떻게 해야 합니까?
$.post AJAX 콜에서 성공 콜백에 가입하지 않았습니다.요청은 실행되지만 결과에 대해서는 아무것도 수행하지 않는다는 것을 의미합니다.결과에 유용한 작업을 수행하려면 다음을 시도합니다.
$.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
});
반면에 리디렉션을 원할 경우 AJAX는 절대 필요하지 않습니다.AJAX는 동일한 페이지에 머물면서 일부만 업데이트하려는 경우에만 사용합니다.
브라우저만 리디렉션하려는 경우:
function foo(id) {
window.location.href = '/Branch/Details/' + id;
}
참고 사항:이런 URL을 하드코딩하면 절대 안됩니다.ASP에서 URL을 처리할 때는 항상 URL 도우미를 사용해야 합니다.NET MVC 응용 프로그램.그래서:
function foo(id) {
var url = '@Url.Action("Details", "Branch", new { id = "__id__" })';
window.location.href = url.replace('__id__', id);
}
이 작업은 보기에 숨겨진 변수를 사용한 다음 해당 변수를 사용하여 자바스크립트 코드에서 게시할 수 있습니다.
여기 보기에 내 코드가 있습니다.
@Html.Hidden("RedirectTo", Url.Action("ActionName", "ControllerName"));
이제 자바스크립트 파일에서 이를 다음과 같이 사용할 수 있습니다.
var url = $("#RedirectTo").val();
location.href = url;
저한테 매력적으로 느껴졌어요.당신에게도 도움이 되길 바랍니다.
다음을 사용할 수 있습니다.
window.location.href = '/Branch/Details/' + id;
그러나 당신의 Ajax 코드는 성공이나 오류 함수가 없으면 불완전합니다.
// 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>
언급URL : https://stackoverflow.com/questions/8148632/redirecting-to-another-page-in-asp-net-mvc-using-javascript-jquery
'IT' 카테고리의 다른 글
반응형 CSS 배경 이미지 (0) | 2023.10.25 |
---|---|
connect/expressjs에서 "서명된" 쿠키란 무엇입니까? (0) | 2023.10.25 |
Android: 뷰에 max Height가 없는 이유는 무엇입니까? (0) | 2023.10.25 |
Swift에서 type def를 선언하려면 어떻게 해야 합니까? (0) | 2023.10.25 |
엔티티 프레임워크에서 네임스페이스 변경 (0) | 2023.10.25 |