Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get placeholder text in HTML input in a scaffolded view by using Display(prompt) attribute over model class

Tags:

I used scaffolding to generate the views and controllers for me and I am using the EF code first syntax.

I understand that T4 templates are responsible for implementing the attributes values in the generated HTML/view code, but I don't see the default scaffolding template available with VS 2015 community edition doing anything for placeholder text.

Per my understanding upon decorating a model property with [Display(Prompt="some placeholder text")] attribute results in the some placeholder text being displayed as placeholder for the input text box in the create/edit views.

But to my dismay, this does not happen.

Is there any other attribute? or something else I need to do? or is it because I used scaffolding to generate views? or is the default T4 template not doing its job very well?

My code for model class looks like below:

public class Status
{
    public int ID { get; set; }

    [Required(ErrorMessage ="Status Name is needed!")]
    [Display(Name ="Status Name",Prompt ="Type something here!")]
    public string StatusName { get; set; }
    [Required]
    public string Description { get; set; }
}

Below is the generated view code:

@model LunchFeedback.Models.Status

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Status</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.StatusName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StatusName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Edit:

I am well aware that directly editing the view file and adding a placeholder holder there can do the job.

        @Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control", placeholder = "Type something here!" } })

But I want to control all things from the model and want to use scaffolding. Preferably even edit/customize T4 template to do so.