开发者

ASP.NET MVC add checkbox dynamically

开发者 https://www.devze.com 2023-02-19 13:10 出处:网络
I have this : <tr> <td>Label</td> <td>@Html.Partial(\"MyPartial\", Model)</td>

I have this :

<tr>
    <td>Label</td>
    <td>@Html.Partial("MyPartial", Model)</td>
</tr>

In "MyPartial" :

@if (Model.MyList.Count == 0){
    Html.CheckBox("chk" + Model.MyList[0].Id);

}
else
{
    Html.Ch开发者_如何转开发eckBox("chk" + Model.MyList[0].Id); <br />
    Html.CheckBox("chkCommon"); <br />
    Html.CheckBox("chk" + Model.MyList[1].Id); <br />
}

I don't see any checkbox appear, when I place some text in, I see it.

Any idea ?

Thanks,


I think you're missing the @ in front of Html.CheckBox().

Try this:

    @Html.CheckBox("chk" + Model.MyList[0].Id); <br />
    @Html.CheckBox("chkCommon"); <br />
    @Html.CheckBox("chk" + Model.MyList[1].Id); <br />

BTW, if your list count is 0, then the block of code inside your if "true" statement will fail because you're trying to reference an item that doesn't exist in your list:

@if (Model.MyList.Count == 0){
    @Html.CheckBox("chk" + Model.MyList[0].Id); // will throw an exception

}
0

精彩评论

暂无评论...
验证码 换一张
取 消