Doubt with mvc list editing, using checkbox and editable field

Asked

Viewed 679 times

5

I’ve got a lot of doubt and I barely know where to start, so I’m going to go to college:

I have a page cshtml (I’m using ). It has a list, assuming that 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 make available for editing the values 'B' and’D', how do I do? And how do I add the checkbox on that list?


EDIT

I’m using this property

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

But it didn’t work. It still allows editing, follows as this my code.

<td> @Html.EditorFor(modelItem => item.obs, new { htmlAttributes = new { @readonly = "readonly" } })</td>
  • After you put the EditorFor, how was the generated HTML? You can edit your question and put this too?

  • <input class="text-box single-line" id="item_obs" name="item.Obs" type="text" value="test test"> this was the generated html.

  • I ended up seeing that the property editorFor does not have properties that disable it, so I switched to textBoxFor, the only apparent change is that all my values will be converted to text

  • It wouldn’t be the case to use @Html.Displayfor for items you don’t want changed?

  • @Yes, you’re right. You’d have to rewrite the template editor to support read-only. I will update my answer.

  • then @Rsinohara, but there is a field where a certain type of user can edit and the other cannot, but the text already helps and "solved" the problem.

Show 1 more comment

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 only 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" } })

To checkbox, use:

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

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


EDIT

@Html.EditorFor() does not support read-only natively (only by reimplementing the Helper), then 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 regarding htmlAttributes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.