Doubt with mvc list editing, using checkbox and editable field

5

I'm a little doubtful and I barely know where to start, so I'm going to turn to college students:

I have a page with% as% (I am using ). It has a list, assuming the elements are A, B, C, D, E. The value of these elements comes from the database and both are already being loaded correctly.

Assuming I want to leave the 'B' and 'D' values available for editing, how do I do it? And how do I add cshtml to this list?

EDIT

I'm using this property

new { htmlAttributes = new { @readonly = "readonly" } } 

But it did not work. It still allows editing, it follows like this my code.

<td> @Html.EditorFor(modelItem => item.obs, new { htmlAttributes = new { @readonly = "readonly" } })</td>
    
asked by anonymous 29.06.2015 / 17:26

1 answer

0

Just render your View using the Helpers HTML. For example:

@Html.EditorFor(model => model.A)
@Html.EditorFor(model => model.B)
@Html.EditorFor(model => model.C)
@Html.EditorFor(model => model.D)
@Html.EditorFor(model => model.E)

Since you just want to leave B and D for editing, use HTML to block access to the field:

@Html.EditorFor(model => model.A, new { htmlAttributes = new { @readonly = "readonly" } })
@Html.EditorFor(model => model.B)
@Html.EditorFor(model => model.C, new { htmlAttributes = new { @readonly = "readonly" } })
@Html.EditorFor(model => model.D)
@Html.EditorFor(model => model.E, new { htmlAttributes = new { @readonly = "readonly" } })

For checkbox , use:

@Html.CheckBoxFor(model => model.MeuCampoBooleano)

If the field already is bool in Model , @Html.EditorFor() already generates checkbox .

EDIT

@Html.EditorFor() does not only support natively reading (only reimplementing Helper ), so the way is to use @Html.TextBoxFor() for text fields.

@Html.TextBoxFor(model => model.A, new { @readonly = "readonly" })
@Html.TextBoxFor(model => model.B)
@Html.TextBoxFor(model => model.C, new { @readonly = "readonly" })
@Html.TextBoxFor(model => model.D)
@Html.TextBoxFor(model => model.E, new { @readonly = "readonly" })

Note that there is a slight syntactic difference between htmlAttributes .

    
29.06.2015 / 17:36