开发者

how to create a checkbox that need to pass string value?

开发者 https://www.devze.com 2023-03-08 04:20 出处:网络
i want to create a check box and that should have to string value? <%:Html.Ch开发者_运维问答eckBoxFor(model => model.bird)%>

i want to create a check box and that should have to string value?

<%:Html.Ch开发者_运维问答eckBoxFor(model => model.bird)%>
<%:Html.LabelFor(model=>model.bird) %>

i get a error saying string cannot convert to bool?


The problem is because the bird member of your model is not a boolean type.

Add a new bool member (birdBool) to your model. This member can be as follow:

public bool birdBool
{
   get{
      try
      {
         return bool.Parse(this.bird);
      }
      catch{
         return false;
      }
   }
   set{
      this.bird=value.ToString();
   }
}

In your view write this:

<%:Html.CheckBoxFor(model => model.birdBool)%>
<%:Html.LabelFor(model=>model.bird) %>
0

精彩评论

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

关注公众号