开发者

When model binding includes a bool what string value must the form collection contain?

If I am using model binding and one of the variables in the model is a bool what string must the form collection contain to be considered true or false?

So for example if my model has a variable:

bool isHappy;

Now when the model binder reads the for开发者_如何学JAVAm collection and it contains name "isHappy" what will the value have to be? "true", "checked", "1", etc?


It will be "true" or "false". Note that it's case insensitive so "True", "False", "TRUE", "FALSE" also work.


FYI - Here's how I create a Y/N select list for this case:

public static class Helpers
{
    public static SelectList GetYesNoSelectList()
    {
        SelectListItem yesChoice = new SelectListItem { Value = "True", Text = "Y" };
        SelectListItem noChoice = new SelectListItem { Value = "False", Text = "N" };
        List<SelectListItem> yesNoList = new List<SelectListItem>();
        yesNoList.Add(yesChoice);
        yesNoList.Add(noChoice);

        return new SelectList(yesNoList, "Value", "Text");
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜