2013-04-17 101 views
0

我有一个场景,我必须将ext textfield的用户名和密码值传递给控制器​​操作才能对数据库进行验证。我写了下面的代码将ext文本框值传递给javascript,然后传递给控制器​​操作

<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/';   

        } 

我如何通过这些值来控制行动?

谢谢!

回答

1

你将不得不使用ajax和jquery来实现这一点。网页标题

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

2.Perform在CallLogin Ajax调用()

1.Include jQuery库functoin

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)); 
     } 
+0

嗨懒鬼..谢谢你的答复..但是当我尝试你的代码..按钮不提交给控制器oller.as这样的网页不提交..当我在IE浏览器尝试相同的..它发射一个错误,说$是未申报..任何想法为什么? – Philomath

+0

你需要在你的页面中包含jQuery库以使其工作。查看我的修改回答 – Sharun

+0

我已将以下代码添加到我的aspx文件..但是代码工作正如预期只在mozilla – Philomath

相关问题