0

I am having difficulties trying to render a checkBox on my Partial View. Basically, what I want is to render the checkBox based on a value extracted from the database. Please see my code below:

        <div class="editor-label">
            @Html.LabelFor(model => model.Active)
        </div>
        <div class="editor-field">
            @{if (Model.Active == 'Y')
              { Html.CheckBox("Active", true); }
              else
              { Html.CheckBox("Active", true); }
                }
        </div>

In this code block, I am checking for the value inside the Active field from my model and renders the isChecked property of the checkBox to true or false based on that value.

I have debugged this code and used a breakpoint. I have a value of 'Y' from my database and it did went through the if statement. However, when the form pops up, the checkBox didn't render.

Can someone please help me? Thanks!

1 Answer 1

1

I think your main problem may be because you have @{ instead of just @ before the if.. try remove that.

also, to make things easier.. create a new property on your view model:

public bool IsActive
{
    get { return Active == "Y"; }
}

then on your view, use Html.CheckBoxFor(m=> m.IsActive)

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

1 Comment

you're welcome. please mark this as "answer" if that is the case. :)

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.