35

I'm using MVC 3 and Razor

At the moment I'm using

@model MyProject.ViewModels.MyViewModel

    @foreach (var item in Model.MyProperty)
{  
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.AdvSlotId }) |
            @Html.ActionLink("Details", "Details", new { id = item.AdvSlotId }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.AdvSlotId })
        </td>
        <td>
            @item.AdvSlotId
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.Description
        </td>
        <td>
             @Html.CheckBox(item.IsPublished, new { @disabled = "disabled" })
        </td>
        <td>
            @item.Notes
        </td>
    </tr>    
}

The VIEW MODEL:

namespace MyProject.ViewModels
{
    public class MyViewModel
    {
        public MyViewModel(List<AdvSlot> advSlots)
        {
            MyProperty= advSlots;
        }

        public List<AdvSlot> MyProperty { get; set; }

    }
}

To display a Check Box for a property in my Model. As I'm doing is wrong so I can only display a text like TRUE.

Could you please tell me how to create the CheckBox with Razor? I would also need have it as READONLY.

Thanks for your help.

GibboK
  • 71,848
  • 143
  • 435
  • 658

6 Answers6

46

Should be something like this:

@Html.CheckBoxFor(m => m.IsPublished, new { @disabled = "disabled" })

UPDATE:

Assuming that your class AdvSlot contains a property IsPublished you can write in your loop:

<td>
    @Html.CheckBox(item.AdvSlotId + "_IsPublished", item.IsPublished, new { @disabled = "disabled" });
</td> 
LeftyX
  • 35,328
  • 21
  • 132
  • 193
35
 @Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })

This should work. However i got a problem with @disabled = "disabled". If i make the checkbox checked by default and make the checkbox disabled at the same time it wont post with its value. At controller it will get false. Using "return false" user wont able to change the value and it will also post the value which has set on load as by default.

Anupam Roy
  • 1,654
  • 2
  • 18
  • 25
  • 6
    Your answer should be accepted. If we use property @disabled="disabled", the textboxfor always passes FALSE to server – Anh Hoang Jun 09 '16 at 02:10
  • @Ahn Hoang I agree with you. This is a perfect answer in my opinion as I have faced same thing you said earlier. – Ananda G Oct 04 '17 at 08:27
  • This is a better solution – Nigel Fds Dec 04 '17 at 04:48
  • 1
    @AnhHoang I believe the server actually just omits values for any disabled controls; it's the model binding on the server that initializes the property of the viewmodel to `false` (the default for boolean fields). – brichins Mar 27 '18 at 00:42
8

I'm using MVC4, you get the same effect with a simple something like

@Html.DisplayFor(m => m.IsPublished)

No need to care to select checkbox or set disabled yourself. Not sure, if this is a new feature in MVC4, so maybe it will work in MVC3 as well.

outofmind
  • 1,430
  • 1
  • 20
  • 37
2

I will just add my case which may be useful to someone.. if you have html attribute too in your checkboxFor then

  @Html.CheckBoxFor(model => model.item, new { onclick = "return false", Title = "is not editable",htmlAttributes = new { @class = "form-control" } })

@Anupam Roy answer will work good.just adding my case.Hope it will be useful.

Abdul Hadee
  • 113
  • 6
0

In your MyViewModel you can remove the setter :

public bool IsPublished 
{
   get {
        //... get from db
       }
}

and in your View :

@Html.CheckBoxFor(m => m.IsPublished, new {@disabled="disabled"})
Mohamed Farrag
  • 1,682
  • 2
  • 19
  • 41
  • this line should give you error @Html.CheckBox(item.IsPublished, new { @disabled = "disabled" }) , it has some invalid arguments – Mohamed Farrag Jul 03 '12 at 11:00
0

In Razor:

<input asp-for="IsStoped" type="checkbox" name="IsStoped" value="@Model.IsStoped" disabled />
Haya Akkad
  • 281
  • 4
  • 15