0

I have a model which has a field

[DisplayName("Receive occasional email notifications about new services or features")]
public bool ReceiveEmail { get; set; }

In my view I want a checkbox which will come checked by default.

I tried this:

<%:Html.CheckBoxFor(m => m.registerModel.ReceiveEmail, new { @checked = "checked" })%>

But did not work...

Any help will be appreciated.

Thanks Arnab

1
  • Why you do not just set the value in your model before passing it to the view? Commented Aug 26, 2011 at 13:31

1 Answer 1

1

The proper way to do this is to set your view model property in the controller action rendering this view:

public ActionResult Foo()
{
    var model = new MyViewModel
    {
        registerModel = new registerModel
        {
            ReceiveEmail = true
        }
    };
    return View(model);
}

Now all you need in your strongly typed view is:

<%= Html.CheckBoxFor(m => m.registerModel.ReceiveEmail) %>

and the checkbox will be automatically checked.

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

1 Comment

+1 I'm not sure this is the "proper way", but it certainly worked. @checked = "checked" certainly does not! Thanks.

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.