简体   繁体   中英

How do I check if a checkbox is checked on the controller page MVC5

Here is the cshtml page

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

I want to check to see if its checked or not in the controller page. How do I go about doing this?

By adding a boolean parameter named IsNewlyEnrolled (case insensitive) to the form's submission endpoint, it will be populated with the state of the checkbox.

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

Alternatively, if you're accepting a complex model, add a field/property to that model with the same name.

public class MyModel
{
    public bool IsNewlyEnrolled { get; set; }
}

public HttpResponseMessage MyEndpoint(MyModel model) {
    // true == checked
    // false == unchecked
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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