将ext文本框值传递给javascript,然后传递给控制器动作

Passing ext textbox values to javascript and then to controller action

本文关键字:然后 控制器 javascript 值传 ext 文本      更新时间:2023-09-26

我有一个场景,我必须将用户名和密码值从ext textfield传递到控制器操作,以与数据库进行验证。我编写了以下代码

   <ext:TextField Name="Username" runat="server" ID="UserName"></ext:TextField>
   <ext:TextField Name="Password" runat="server" ID="Password"></ext:TextField>
   <ext:Button runat="server" ID="submit1" Type="Submit"  Text="LOGIN" >
    <Listeners>
        <Click Fn="CallLogin">
        </Click>
    </Listeners>
    </ext:Button>

CallLogin() {

        var username = document.getElementById('UserName').value;
        var password = document.getElementById('Password').value;
        //var url = '@Url.Action("Login", "Index")';
        //window.location.href = url;
        window.location.href = '/Login/Index/';          
                   }

我如何将这些值传递给控制器动作?

谢谢!

您必须使用ajax和jquery来实现这一点。

1。在你的页面头包含jquery库

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> 
</head>

2。在CallLogin()函数

中执行ajax调用
function CallLogin() {
        var username = document.getElementById('UserName').value;
        var password = document.getElementById('Password').value;
  $.ajax({
                    type: "POST",
                    url: '/Login/Index/',
                    data: { userText: username , passwordText: password },
                    success: function (response) {
                        if (response > 0) {
                           alert("Success");
                          }
                        else
                         {
                        alert("Login failed");
                           }
    });
}

假设你的动作方法看起来像这样:

  [AcceptVerbs(HttpVerbs.Post)]
        public int Index(string userText, string passwordText)
        {
           return (checkValidUser(userText,passwordText));
        }