這之後,我想用戶登錄,然後查看離開:如何使重定向用戶,他們已經提交了一個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
更新代碼[here](http://stackoverflow.com/questions/23725155/issue-setting-values-in-model-using-ajax-and-then-redirecting-to-a - 新頁面)但有一個問題。 – ValMangul