asp.net-mvc – 如何添加“必需”属性到mvc 5剃刀视图文本输入编
发布时间:2020-12-16 00:40:01  所属栏目: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方法中调用ResolveClientUrl(ASPNet W
 - asp.net-mvc – 如何禁用自动完成在MVC Html助手
 - asp.net – 当使用HttpContextScoped()时,StructureMap不会
 - asp.net-core – 如何在Azure Web Apps中将redirect_uri协议
 - asp.net – MVC 4模型回归null
 - asp.net-mvc-3 – 在Razor中加入文本变量
 - asp.net-core – 如何使用Identity Server 4使用桌面/移动应
 - asp.net-mvc-3 – 如何为MVC3生成服务和存储库层
 - DateTime数据注释在ASP.NET Core RC2应用程序中不起作用
 - asp.net – AngularJs 2与ASP.NET 4.5.1
 
推荐文章
            站长推荐
            - asp.net – 寻找用于呈现显示对象视图的表单的代
 - asp.net-mvc – 将List参数传递到ASP.NET MVC3中
 - asp.net-mvc – 如何使用ajax get或post在带有参
 - asp.net-mvc – 如何本地化枚举并使用类似于Html
 - asp.net-mvc – 为什么我的IIS Express Web应用程
 - asp.net – 在POST时丢失HTML表单数据
 - asp.net-mvc-2 – iframe,跨域Cookie,p3p策略和s
 - asp.net-ajax – 建议使用不引人注意的AJAX与MVC
 - asp.net – Telerik RadDatePicker在2030年之后没
 - Asp.Net动态数据的未来
 
热点阅读
            