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(); 
    } 

} 

я не включил мод el, поскольку я полагаю, что это не проблема, но я могу добавить ее. Модель называется SSH.cs

+0

Обновленный код [здесь] (http://stackoverflow.com/questions/23725155/issue-setting-values-in-model-using-ajax-and-then-redirecting-to-a -new-page) Но с проблемой. – 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 (м => м. Имя пользователя) @ Html.HiddenFor (т => т. Пароль) @ Html.HiddenFor (т => т. Ipaddress) на ваш взгляд cheers

+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