2014-05-18 59 views
0

這之後,我想用戶登錄,然後查看離開:如何使重定向用戶,他們已經提交了一個Ajax調用

@model IEnumerable<TerminalHost.Models.deviceInfo> 

<script> 
    $(document).ready(function() { 
     $("#myTerminal").dialog({ 
     autoOpen: false, 
     width: 550, 
     height: 350, 
     show: { 
      effect: "fade", 
      duration: 650 
     }, 
     hide: { 
      effect: "fade", 
      duration: 600 
     }, 
     buttons: { 
      "OK": function() { 
        //Get he content from the input box 
        var uName = document.getElementById("usernameInput").value; 
        var pWord = document.getElementById("passwordInput").value; 
        var ipAddress = document.getElementById("ipAddressInput").value; 
        $.ajax({ 
         type: "POST", 
         url: "/Terminal/terminalLogin", 
         data: { userN: uName, passW: pWord, ipAd: ipAddress}, // pass the data to the method in the Terminal Contoller 
         success: function (data) { 
          window.location.href = data.redirectToUrl; 
         }, 
         error: function (e) { alert(e); } 
        }) 
       }, 
      "Cancel": function (e) { 
       $("#myTerminal").dialog("close"); 
      } 
     } 
    }); 


    $("#terminalAccess").click(function form() { 
     $("#myTerminal").dialog("open"); 
    }); 
}); 

function onSuccess() { 
    $("#myTerminal").dialog("close"); 
} 
</script> 

<div id="myTerminal" title="Terminal Login"> 
    <label for="username">Username</label> 
    <input type="text" name="passwordInput" id="usernameInput" /> 
    <label for="password">Password</label> 
    <input type="password" name="password" id="passwordInput" /> 
    <label for="ipAddress">Ip Address</label> 
    <input type="text" name="ipAddress" id="ipAddressInput" /> 
</div> 

這是視圖,用戶也去:

@{ 
    ViewBag.Title = "Terminal"; 
} 
<script> 
    $(document).ready(function() { 
    $("#cmdSend").click(function() { 
     // Get he content from the input box 
     var mydata = document.getElementById("cmdInput").value; 
     $.ajax({ 
      type: "POST", 
      url: "/Terminal/processCommand", 
      data: { cmd: mydata }, // pass the data to the method in the Terminal Contoller 
      success: function (data) { 
       //alert(data); 
       // we need to update the elements on the page 
       var existingHtml = $(".terminal").html(); 
       $(".terminal").append("</br>" + "->" + mydata + "</br>" + data); 
      }, 
      error: function (e) { alert(e); } 
     }) 
    }); 
}); 
</script> 
<h3>Terminal for device: *Device name*</h3> 
<ol class="round"> 
<li> 

    <article> 
     <div class="terminal" style="overflow-y: scroll;" ></div> 
     <div class="terminalInputArea"> 
      <div class="inputBox"> 
      <input id="cmdInput" /><button type="button" id="cmdSend">Send</button> 
      </div> 
     </div> 
    </article> 

</li> 
</ol> 

,這是我想要的數據發送到控制器:

public class TerminalController : Controller 
{ 
    SSH ssh = new SSH(); 

    [HttpPost] 
    public ActionResult terminalLogin(string userN, string passW, string ipAd) 
    { 
     ssh.username = userN; 
     ssh.password = passW; 
     ssh.ipAddress = ipAd; 

     if (Request.IsAjaxRequest()) 
     { 
      return Json(new { redirectToUrl = Url.Action("Terminal") }); 
     } 
     return RedirectToAction("Terminal"); 
    } 

    [HttpPost] 
    public string processCommand(string cmd) 
    { 
     ssh.cmdInput = cmd; 
     String info = ssh.SSHConnect(); 
     info = info.Replace("!", "<br>"); 
     return info; 
    } 

    public ActionResult Terminal() 
    { 
     return View(); 
    } 

} 

我不包括國防部埃爾,因爲我認爲它不是問題,但我可以添加它。該模型被稱爲SSH.cs

+0

更新代碼[here](http://stackoverflow.com/questions/23725155/issue-setting-values-in-model-using-ajax-and-then-redirecting-to-a - 新頁面)但有一個問題。 – ValMangul

回答

0

您可以使用「window.location.href = data.redirect;」

$.ajax({ 
        type: "POST", 
        url: "/Terminal/Terminal", 
        data: { userN: uName, passW: pWord, ipAd: ipAddress}, // pass the data to the method in the Terminal Contoller 
        success: function (data) { 
         window.location.href = data.redirect; 

         //????? I'm assuming the redirect goes here. 
         alert(data); 
        }, 
        error: function (e) { alert(e); } 
       }) 

你的類應該是這樣的

public class TerminalController : Controller 
{ 
SSH ssh = new SSH(); 

    [HttpPost] 
public ActionResult terminalLogin(string userN, string passW, string ipAd) 
{ 
    ssh.username = userN; 
    ssh.password = passW; 
    ssh.ipAddress = ipAd; 

    if (Request.IsAjaxRequest()) 
    { 
     return Json(new { redirectToUrl = Url.Action("Terminal","Your controller", new {parametername_Username=userN, parametername_Password= passW,parametername_IpAddress = ipAd }) }); 
    } 
    return RedirectToAction("Terminal"); 
} 

[HttpPost] 
public string processCommand(string cmd) 
{ 
    ssh.cmdInput = cmd; 
    String info = ssh.SSHConnect(); 
    info = info.Replace("!", "<br>"); 
    return info; 
} 

public ActionResult Terminal() 
{ 
    return View(); 
} 

}

data.redirect是,你應該從控制器發送URL。如果你需要更多的細節,請讓我知道。

添加下面的元素到視圖 @ Html.HiddenFor(M =>米。用戶名) @ Html.HiddenFor(M =>米。密碼) @ Html.HiddenFor(M =>米。ip地址) 對你的看法 乾杯

+0

我曾試過[這裏](http://stackoverflow.com/questions/23725155/issue-setting-values-in-model-using-ajax-and-then-redirecting-to-a-new-page)但我注意到,當我重定向我爲用戶名設置的信息時,密碼和Ip被取消。 – ValMangul

+0

你有什麼想法如何使用戶重定向時的數據傳遞? – ValMangul

+0

如果我明白你問了什麼,你需要知道如何在數據上創建重定向不是嗎?它是返回json對象的屬性。讓我知道你需要一個示例代碼。 如果我沒有得到你的問題,你可以詳細說明它。對此大赦。 – PEO

相關問題