@ Html.CheckBoxFor razor problem?

I have a form that responds to a submit button.

I have one checkbox of mine:

  @Html.CheckBoxFor(m => m.CheckBoxValue)

      

How can I get if checked is YES, otherwise NO is preferred as a string in my model.

  public bool CheckBoxValue { get; set; } OR
  public string CheckBoxValue { get; set; }

      

Please, help.

thank

Update

This was the way.

   @Html.CheckBoxFor(m => m.CheckBoxValue)

   public bool CheckBoxValue { get; set; } 

      

+3


source to share


1 answer


Are you just looking for a convenient value display? This should happen in the view, not the model. Something like that:

@Html.CheckBoxFor(m => m.CheckBoxValue)
<!-- some other markup, blah blah blah -->
@(Model.CheckBoxValue ? "Yes" : "No")

      

As far as possible, the model should only contain the structure and logical functionality of the data. Any user experience or anything that users see and interact with should appear in the view.

Edit:



Based on your comment, you can add something like this model:

public string CheckBoxDisplayValue
{
    get
    {
        return CheckBoxValue ? "Yes" : "No";
    }
}

      

Note. It is an addition to a property bool

CheckBoxValue

that is associated with the view, not instead of it. The model needs a boolean value, so it CheckBoxValue

is boolean. All this means adding only a read-only property to the model to display a convenient display for the boolean value.

But this is not recommended. It looks like you have an end goal that you are not asking here and is probably the best way to achieve that end goal.

+3


source







All Articles