Reputation: 47
I'm currently writing an ASP.Net MVC application
I've successfully made an input box in CSHTML.
@Html.EditorFor(model => model.Test, new { htmlAttributes = new { @class = "form-control"} })
I now want to take the content of a string from the controller and add is as placeholder text. I've tried the following
@Html.EditorFor(model => model.Test, new { htmlAttributes = new { @class = "form-control", placeholder = @Html.DisplayFor(model => model.Test) } })
But that set the placeholder like so: placeholder=""
If I instead use @Html.DisplayNameFor
it sets the placeholder to the variable's name Test
, but I want the actual string content. I'm aware that one can use the Viewbag to pass text to the view, but there must be a way of doing it this way.
Any help is greatly appreciated.
Upvotes: 1
Views: 4209
Reputation: 8871
Make a Property in your Model which you are passing to view.
Assign the string into that property.
Model.SomeName= YourVariableName;
Then Try Like This :-
@Html.EditorFor(model => model.Test, new { htmlAttributes = new { @class = "form-control", placeholder = @Model.SomeName }
Upvotes: 0
Reputation: 44
I think you need to add the [Display (name="Your Text")] attribute in the propriety of the model like this:
[Display (Name="Name Of Client:")]
Public string Name {get; set;}
Upvotes: 0
Reputation: 5284
You can't use @Html.DisplayFor()
as a placeholder but you can use Html.DisplayNameFor()
.
Example:
@Html.EditorFor(model => model.Test, new { htmlAttributes = new { @class = "form-control", @placeholder = "Your Place Holder" } })
OR
@Html.EditorFor(model => model.Test, new { htmlAttributes = new { @class = "form-control", placeholder = @Html.DisplayNameFor(model => model.Test) } })
Upvotes: 1