Dhr*_*shi 6 c# placeholder asp.net-mvc-5
我使用脚手架为我生成视图和控制器,我使用EF代码第一语法.
我知道T4模板负责在生成的HTML /视图代码中实现属性值,但我没有看到VS 2015 community edition为占位符文本做任何事情的默认脚手架模板.
根据我的理解,在使用[Display(Prompt="some placeholder text")]属性装饰模型属性时,会在some placeholder text创建/编辑视图中将输入文本框显示为占位符.
但令我沮丧的是,这不会发生.
还有其他属性吗?还是我需要做的其他事情?或者是因为我使用脚手架来生成视图?或者是默认的T4模板没有很好地完成它的工作?
我的模型类代码如下所示:
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; }
}
Run Code Online (Sandbox Code Playgroud)
下面是生成的视图代码:
@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")
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我很清楚,直接编辑视图文件并添加占位符持有者可以完成这项工作.
@Html.EditorFor(model => model.StatusName, new { htmlAttributes = new { @class = "form-control", placeholder = "Type something here!" } })
Run Code Online (Sandbox Code Playgroud)
但我想控制模型中的所有东西,并希望使用脚手架.最好甚至编辑/定制T4模板来这样做.
小智 5
该Prompt财产DisplayAttribute的价值添加到WaterMark该物业ModelMetadata与物业有关.它不用于生成html placeholder属性.
您希望将其值用作placeholder属性,但另一个开发人员可能希望使用它来生成工具提示(使用该title属性),因此它可以让开发人员知道如何使用它.
用于脚手架编辑视图的T4模板为@Html.Editor()模型的属性生成,因此您可以EditorTemplates在/Views/Shared/EditorTemplates文件夹中创建自己的/Views/youControllerName/EditorTemplates模板(或者如果您希望将特定模板用于特定控制器)
请注意,该EditorFor()方法首先在中搜索模板/Views/youControllerName/EditorTemplates.如果找不到,则搜索/Views/Shared/EditorTemplates并且如果找不到,则使用默认值EditorTemplate
例如,要为typeof的所有属性使用模板string,请创建String.cshtml在EditorTemplates文件夹中命名的部分视图
@Html.TextBox("", ViewData.ModelMetadata.Model, new { placeholder = ViewData.ModelMetadata.Watermark })
Run Code Online (Sandbox Code Playgroud)
或者仅限于将此模板用于某些属性,将其命名为partial(例如)PlaceHolder.cshtml,然后使用UIHintAttribute您的属性
[Display(Name ="Status Name", Prompt ="Type something here!")]
[UIHint("PlaceHolder")]
public string StatusName { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1564 次 |
| 最近记录: |