11

When I am just deleting the editor fields that I dont' want the user to see in the View , their data becomes null, I want to keep the data, but ,I don't want the user to edit it.

  <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>


            <div class="editor-label">
                @Html.LabelFor(model => model.parent_directory)
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => model.parent_directory)
            </div>

            <p>
                <input type="submit" value="Add" />
            </p>

The @Html.EditorFor I am referring to is model.parent_directory. Can anyone suggest a solution that doesn't involve js ? I tried using DisplayFor instead of EditorFor and I still get a null value.

Dave Demirkhanyan
  • 570
  • 1
  • 5
  • 22

4 Answers4

19

Please take a look at this MSDN page. http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor(v=vs.118).aspx

According to that page, you can't provide html attributes as a parameter for obvious reasons. Because EditorFor can be an input field or select and their html attributes differs from one another.

If you just want to hide it temporarily and show it later to the user, you can use @Html.TextBoxFor. You can find out more about TextBoxFor overloads at http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx

e.g:

@Html.TextBoxFor(model => model.Name, new {style = 'display:none'})

if you want to keep these details hidden throughout the process, you can use @Html.HiddenFor

e.g

@Html.HiddenFor(model => model.Name)
Amila
  • 3,711
  • 3
  • 26
  • 42
7

Use hidden fields for this:

@Html.HiddenFor(model => model.Name)
Pavel
  • 526
  • 2
  • 5
1

If you want to display the fields and don't allow users to edit, you can make them read only..

@Html.EditorFor(model => model.Name, new {@readonly = "readonly" })

If you don't want to show the fields to user and just need to hold data you can use

@Html.HiddenFor(model => model.Name)
tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
1

You can do this with:

@Html.Editor("WContractID", new { htmlAttributes = new { @class = "form-control", @style = "display: none" } })
kwoxer
  • 3,734
  • 4
  • 40
  • 70
RafiO
  • 113
  • 1
  • 3
  • 9
  • Format your code section and provide some explanation around your answer. Please note that your answer looks similar to highly voted/accepted answer for this question. So improve your answer. – mmushtaq Jan 09 '17 at 06:59
  • This answer contradicts the accepted answer, and should be the accepted answer. – mxmissile Nov 15 '17 at 16:42