have razor text box default value disappear

2019-06-05 22:40发布

问题:

I have a form with two text boxes. What I would like is when the user clicks in the text boxes the default values disappears. I am using razor so not sure how to add the onfocus event I think I need.

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "newsletterform" }))
{
    @Html.TextBoxFor(m => m.Name, new { @Value = "Name"})

    @Html.TextBoxFor(m => m.Email, new { @Value = "Email"})

    <input type="submit" class="newsletterGo" value="Go" />
}  

回答1:

You can user the placeholder attribute. An example of it is the search box at the top of this page

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "newsletterform" }))
{
    @Html.TextBoxFor(m => m.Name, new { placeholder = "Default Name"})

    @Html.TextBoxFor(m => m.Email, new { placeholder = "person@example.com"})

    <input type="submit" class="newsletterGo" value="Go" />
}  

Also, you don't need to specify the @Value attribute. The HTML helpers take care of setting the input values for you.



标签: razor textbox