大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 如何添加“必需”属性到mvc 5剃刀视图文本输入编辑器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@Html.TextBoxFor(m => m.ShortName,new { @class = "form-control",@placeholder = "short name"})
我需要这个字段是必需的(即当用户导航而不放置价值旅馆时,有一个红色的轮廓)。在WebForms HTML 5中,我只能说< input type =“text”required />有这个效果。
使用Razor语法完成此功能的正确语法是什么?
@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-mvc – 如何添加“必需”属性到mvc 5剃刀视图文本输入编辑器全部内容,希望文章能够帮你解决asp.net-mvc – 如何添加“必需”属性到mvc 5剃刀视图文本输入编辑器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。