【问题标题】:Capture Textbox value through a CheckBox asp.net MVC通过 CheckBox asp.net MVC 捕获文本框值
【发布时间】:2018-01-22 03:47:26
【问题描述】:

有一个由文本框组成的表单,这些文本框由 CheckBox 激活或停用 ....

如何捕获用户输入的值以验证支票已被标记?

View

我的观点:

  <script type="text/javascript">
        $(function (){
            $("#idcheckproveedor").change(function(){
                var st = this.checked;
                if (st) {
                    $("#txtSearch").prop("disabled", false);
                }
                else {
                    $("#txtSearch").prop("disabled", true);
                }
                });
            });
    </script>

  <div class="form-group">
  <label>Proveedor</label>
  <input type="checkbox" id="idcheckproveedor" checked="checked" />
  @Html.TextBox("searchTerm", null, new { @class = "form-control", id = "txtSearch" })
 </div>

【问题讨论】:

    标签: c# asp.net-mvc checkbox textbox


    【解决方案1】:

    您可以为您的复选框元素命名,因为值将使用 name 属性作为键作为键值对发布,因此您可以在视图中执行以下操作:

    <input type="checkbox" name="AllowSearch" id="idcheckproveedor" checked="checked" />
    @Html.TextBox("searchTerm", null, new { @class = "form-control", id = "txtSearch" })
    

    实际上,您将拥有一个名为 AllowSearch 的复选框参数,该参数的类型为 bool,用于在发布时保存复选框是否被选中:

    [HttpPost]
    public ActionResult BuscarRecepcion(string searchTerm, bool AllowSearch_
    { 
    
        // validate here 
    
        if(AllwoSearch)
        {
           // do search
        }
    
        // else send back to view as validation failed.
    
    }
    

    【讨论】:

    • 我收到错误:参数字典包含方法“System.Web.Mvc.ActionResult BuscarRecepcion(System.String) 的不可空类型“System.Boolean”的参数“AllowSearch”的空条目, System.Nullable`1[System.Int32], System.String, System.String, Boolean)' @Ehsan Sajjad
    • 您还需要其他必需的参数,我只是向您展示了传递复选框状态的示例
    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 2018-07-02
    • 1970-01-01
    • 2023-03-03
    • 2023-03-21
    • 2017-05-24
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多