Reputation: 1290
I have a form that respond to a submit button.
I have my one checkbox as following:
@Html.CheckBoxFor(m => m.CheckBoxValue)
How can I get if checked YES otherwise NO preferably as string in my model ..
public bool CheckBoxValue { get; set; } OR
public string CheckBoxValue { get; set; }
Please help.
Thanks
Update
This was the way to go.
@Html.CheckBoxFor(m => m.CheckBoxValue)
public bool CheckBoxValue { get; set; }
Upvotes: 3
Views: 3580
Reputation: 218837
Are you just looking for a user-friendly display of the value? That should happen in the view, not in the model. Something like this:
@Html.CheckBoxFor(m => m.CheckBoxValue)
<!-- some other markup, blah blah blah -->
@(Model.CheckBoxValue ? "Yes" : "No")
As much as possible, the model should contain only the structure and logical functionality of the data. Any user-friendliness or anything that users see and interact with should go in the view.
Edit:
Based on your comment, you could add something like this to the model:
public string CheckBoxDisplayValue
{
get
{
return CheckBoxValue ? "Yes" : "No";
}
}
Note: This is in addition to the bool
property CheckBoxValue
which is what gets bound to the view, not in place of it. The model needs a boolean, so CheckBoxValue
is that boolean. All this does is add a read-only property to the model to show a user-friendly display for the boolean value.
But this is not recommended. It sounds like you have an end goal that you're not specifying in the question here, and there's probably a better way to achieve that end goal.
Upvotes: 3