Pass values of checkBox to controller action in asp.net mvc4

后端 未结 12 1525
长发绾君心
长发绾君心 2020-12-14 00:31

I want to test if the checkbox is checked or not from my action method, what i need is to pass checkbox value from view to controller.

This is my view:



        
12条回答
  •  天涯浪人
    2020-12-14 01:25

    You should be strongly typing your views. Then you can do this:

    public class YourViewModel {
        public bool ConditionaValue { get; set; }
    }
    

    In your view, you can create a checkbox that will bind to this boolean value:

    @Html.CheckBoxFor(x => x.ConditionalValue)
    

    If it is checked, the model property will be true.

    For your immediate problem though.. you need to name your checkbox to be the same name as your action method parameters.. and they should be bool..

提交回复
热议问题