4

model

[DefaultValue(true)]
public bool IsActive { get; set; }

view

@Html.CheckBoxFor(model => model.IsActive)

html output and that is my expected

<input data-val="true" data-val-required="Yayında alanı boş bırakılmamalıdır!" id="IsActive" name="IsActive" type="checkbox" value="true" class="valid">

but checkbox is not checked. I cant understand why? What am I missing?

Thanks.

1
  • The 'best practice" in this case, imho, would be to set the value in the controller. ie model.IsActive = true; Commented Feb 15, 2013 at 21:31

1 Answer 1

2

Per the MSDN

A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for trick. I solve it in getter block. get{ return true;}
Setting it in the getter would cause the value to always be true, just as the code says. You could never set the checkbox to false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.