开发者

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

开发者 https://www.devze.com 2023-03-19 14:50 出处:网络
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?

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

精彩评论

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

关注公众号