【问题标题】:How do I check if a checkbox is checked on the controller page MVC5如何检查控制器页面 MVC5 上是否选中了复选框
【发布时间】:2017-04-24 16:19:54
【问题描述】:

这是cshtml页面

<div class="form-group">
    @Html.Label("Please tick this box if you are signing up as a Student")
    @Html.CheckBoxFor(m => m.isNewlyEnrolled)
</div>

我想检查它是否在控制器页面中被选中。我该怎么做?

【问题讨论】:

    标签: c# checkbox asp.net-mvc-5 checkboxfor


    【解决方案1】:

    通过向表单的提交端点添加一个名为IsNewlyEnrolled(不区分大小写)的布尔参数,它将填充复选框的状态。

    public HttpResponseMessage MyEndpoint(bool isNewlyEnrolled) {
        // true == checked
        // false == unchecked
    }
    

    或者,如果您接受复杂模型,请向该模型添加同名的字段/属性。

    public class MyModel
    {
        public bool IsNewlyEnrolled { get; set; }
    }
    
    public HttpResponseMessage MyEndpoint(MyModel model) {
        // true == checked
        // false == unchecked
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2017-03-20
      • 2014-09-14
      • 2013-12-25
      • 1970-01-01
      • 2019-11-19
      相关资源
      最近更新 更多