質問

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!

役に立ちましたか?

解決

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)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top