简体   繁体   English

如何将文本框的值传递给控制器​​中的方法?

[英]How to pass value of textbox to a method in controller?

In Angularjs, I'm trying to pass a value from a textbox to method written in controller as below 在Angularjs中,我尝试将值从文本框传递到控制器中编写的方法,如下所示

#Try 1 #尝试1

<input type="text" ng-blur="isExists(this.value)">

and within my controller I have 在我的控制器内

 $scope.isExists = function (theValue) {
    alert(theValue);
  };

It is not working. 它不起作用。

#Try 2 #尝试2

<input type="text" ng-model="username" ng-blur="isExists()">

and within controller 并在控制器内

$scope.isExists = function () {
        alert($scope.username); // returns undefined
      };

How to pass value from ng-blur to a method within a Controller? 如何将值从ng-blur传递到Controller中的方法?

Updates: 更新:

Any reason why the value is not seen in the textbox? 为什么在文本框中看不到该value

<input type="text" ng-model="username" ng-blur="isExists()">

Fiddle 小提琴

Try2 should not return undefined if the <input type="text" 如果<input type="text" Try2不应返回undefined

is filled with at least one character. 至少包含一个字符。

You can append {{ username }} on the html page just for debug purporses to make sure it was well binded. 您可以在html页面上附加{{username}},仅用于调试目的,以确保其绑定正确。

<input type="text" name="userId" id="txtid" />

<script type="text/javascript">
    $(document).ready(function () {

        $('#txtid').blur(function () {
            debugger;
            var id = $('#txtid').val();

            window.location = "/Home/CreateSupplier/?userid=" + id;

        });
    });
</script>

In controller 在控制器中

 public ActionResult CreateSupplier(string userid)
        {
            if (userid != null)
            {
                return Content("Received Username:" + userid);
            }
            return View(thesupplierList);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM