asp.net-mvc – 如何添加“必需”属性到mvc 5剃刀视图文本输入编辑器
发布时间:2020-05-23 15:53:30 所属栏目:asp.Net 来源:互联网
导读:我有以下MVC 5 Razor HTML助手: @Html.TextBoxFor(m = m.ShortName, new { @class = form-control, @placeholder = short name}) 我需要这个字段是必需的(即当用户导航而不放置价值旅馆时,有一个红色的轮廓)。在WebForms HTML 5中,我只能说 input type =“
|
我有以下MVC 5 Razor HTML助手: @Html.TextBoxFor(m => m.ShortName,new { @class = "form-control",@placeholder = "short name"})
我需要这个字段是必需的(即当用户导航而不放置价值旅馆时,有一个红色的轮廓)。在WebForms HTML 5中,我只能说< input type =“text”required />有这个效果。 解决方法如果需要,您可以使用所需的html属性:@Html.TextBoxFor(m => m.ShortName,placeholder = "short name",required="required"}) 或者您可以使用.Net中的RequiredAttribute类。使用jQuery,RequiredAttribute可以在前端和服务器端进行验证。如果你想去MVC路线,我建议你阅读Data annotations MVC3 Required attribute。 要么 你可以得到真正的进步: @{
// if you aren't using UnobtrusiveValidation,don't pass anything to this constructor
var attributes = new Dictionary<string,object>(
Html.GetUnobtrusiveValidationAttributes(ViewData.TemplateInfo.HtmlFieldPrefix));
attributes.Add("class","form-control");
attributes.Add("placeholder","short name");
if (ViewData.ModelMetadata.ContainerType
.GetProperty(ViewData.ModelMetadata.PropertyName)
.GetCustomAttributes(typeof(RequiredAttribute),true)
.Select(a => a as RequiredAttribute)
.Any(a => a != null))
{
attributes.Add("required","required");
}
@Html.TextBoxFor(m => m.ShortName,attributes)
}
或者如果您需要多个编辑器模板: public static class ViewPageExtensions
{
public static IDictionary<string,object> GetAttributes(this ViewWebPage instance)
{
// if you aren't using UnobtrusiveValidation,don't pass anything to this constructor
var attributes = new Dictionary<string,object>(
instance.Html.GetUnobtrusiveValidationAttributes(
instance.ViewData.TemplateInfo.HtmlFieldPrefix));
if (ViewData.ModelMetadata.ContainerType
.GetProperty(ViewData.ModelMetadata.PropertyName)
.GetCustomAttributes(typeof(RequiredAttribute),true)
.Select(a => a as RequiredAttribute)
.Any(a => a != null))
{
attributes.Add("required","required");
}
}
}
那么在你的模板中 @{
// if you aren't using UnobtrusiveValidation,don't pass anything to this constructor
var attributes = this.GetAttributes();
attributes.Add("class","form-control");
attributes.Add("placeholder","short name");
@Html.TextBoxFor(m => m.ShortName,attributes)
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 休息 – ASP.NET Web Api路由自定义
- 使用ASP.NET WebForms的xVal示例?
- asp.net – NHibernate – ManagedWebSessionContext和WebS
- asp.net-mvc – 更新到MVC 5后,iframe不再有效
- asp.net-mvc – Sql Views的流畅Nhibernate映射
- asp.net-mvc – Internet Explorer缓存asp.netmvc ajax结果
- asp.net-mvc – ASP.NET MVC脚手架很慢
- asp.net-mvc – 如何在ASP.NET MVC 4中使用域组作为具有流畅
- asp.net-mvc – 自定义控制器工厂,依赖注入/结构图问题与AS
- asp.net-mvc – 如何单元测试应用了[Authorize]属性的控制器
推荐文章
站长推荐
- asp.net-mvc-4 – 使用KNOCKOUT.JS和ASP.NET MVC
- 一个ASP.NET应用程序(意外)在多个应用程序域或频
- asp.net – RegisterForEventValidation只能在渲
- asp.net – 为什么有2个web.config文件
- .NET的标记SO如何在飞行中呈现?
- asp.net – HTTP错误403 – 禁止
- asp.net-core – Controller.json设置Serializat
- asp.net – jqgrid jsonReader配置
- ASP.NET MVC3角色和权限管理 – 具有运行时权限分
- asp.net-mvc – ASP.NET MVC – 在ActionFilter中
热点阅读
