Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make @Html.EditorFor invisible?

Tags:

asp.net-mvc

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.

like image 801
Dave Demirkhanyan Avatar asked Nov 10 '14 11:11

Dave Demirkhanyan


People also ask

What is HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property. The following table list the data types and releted HTML elements: DataType.

How to use hidden field in MVC 4 razor?

You can still employ input type="hidden" , but initialize it with a value from the ViewData or for Razor, ViewBag . Then you can pass data between the client and server via ajax. For example, using jQuery: $.


1 Answers

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)
like image 117
Amila Avatar answered Oct 04 '22 03:10

Amila