繁体   English   中英

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

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

在Angularjs中,我尝试将值从文本框传递到控制器中编写的方法,如下所示

#尝试1

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

在我的控制器内

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

它不起作用。

#尝试2

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

并在控制器内

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

如何将值从ng-blur传递到Controller中的方法?

更新:

为什么在文本框中看不到该value

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

小提琴

如果<input type="text" Try2不应返回undefined

至少包含一个字符。

您可以在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>

在控制器中

 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