Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing text for checkboxfor in razorview

USING ASP .Net MVC 4 , Razor View

I have a CheckBoxFor:

    <div class="editor-checkbox-field">
        @Html.CheckBoxFor(model => model.Is2ndMailBody, new { @name = "secMailBody", @id = "secMailBody", @class = "secMailBody", @onclick = "disable(this.form.secMailBody)", @value = "Create Table", @text = "Create Table" })
    </div>

But the CheckBox itself is showing. How can i show a text what the CheckBox for?

my model has :

public bool IsChecked { get; set; }

javascript is working fine and css also. I just want to know how i can show a text for this checkbox?

EDIT 1 :

This is what is showing

I want the text Create Second Mail Body in the right side of the check box.

like image 986
Abdur Rahim Avatar asked Feb 06 '13 12:02

Abdur Rahim


3 Answers

Answer 1: Just put the string right after the CheckBox like below.

<div class="checkbox">
    <label>
        @Html.CheckBoxFor(m => m.Is2ndMailBody) Create Second Mail Body

    </label>
</div>

Answer 2: In Model,

[Display(Name = "Create Second Mail Body")]
public bool Is2ndMailBody{ get; set; }

In View

<div>
    @Html.CheckBoxFor(m => m.IsChecked)
    @Html.LabelFor(m => m.Is2ndMailBody)
</div>
like image 89
Abdur Rahim Avatar answered Oct 20 '22 22:10

Abdur Rahim


First add filter to your model that called DisplayAttribute

[Display(Name = "Some Text")]
public bool IsChecked { get; set; }

And you can use Label for model

@Html.LabelFor(model => model.Is2ndMailBody)
like image 6
AliRıza Adıyahşi Avatar answered Oct 20 '22 21:10

AliRıza Adıyahşi


You can use

@Html.DisplayNameFor

instead of

@Html.LabelFor 

in Alizra's answer. This way, .label css style won't be applied to your text.

like image 6
Ryan Avatar answered Oct 20 '22 22:10

Ryan