簡體   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