Eug*_*erg 42 asp.net-mvc html-helper data-annotations razor
我有以下MVC 5 Razor HTML帮助器:
@Html.TextBoxFor(m => m.ShortName,
new { @class = "form-control", @placeholder = "short name"})
Run Code Online (Sandbox Code Playgroud)
我需要这个字段是必需的(例如,当用户导航而没有放置值值时,会有一个红色轮廓).在WebForms HTML 5中我可以说<input type="text" required />
具有这种效果.在Razor语法中实现此功能的正确语法是什么?
Eri*_*ips 81
如果需要,可以使用required
html属性:
@Html.TextBoxFor(m => m.ShortName,
new { @class = "form-control", placeholder = "short name", required="required"})
Run Code Online (Sandbox Code Playgroud)
或者您可以在.Net中使用RequiredAttribute类.使用jQuery,RequiredAttribute
可以在前端和服务器端验证.如果你想进入MVC路线,我建议阅读Data annotations MVC3 Required属性.
要么
你可以变得非常先进:
@{
// 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)
}
Run Code Online (Sandbox Code Playgroud)
或者如果您需要多个编辑器模板:
public static class ViewPageExtensions
{
public static IDictionary<string, object> GetAttributes(this WebViewPage 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的模板中:
@{
// 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)
}
Run Code Online (Sandbox Code Playgroud)
更新1(对于不熟悉ViewData的Tomas).
摘抄:
所以基本上它(ViewBag)取代魔术字符串:
Run Code Online (Sandbox Code Playgroud)ViewData["Foo"]
具有魔力属性:
Run Code Online (Sandbox Code Playgroud)ViewBag.Foo
Flo*_*min 19
在您的模型类上使用[Required]
属性装饰该属性.即:
[Required]
public string ShortName {get; set;}
Run Code Online (Sandbox Code Playgroud)
在.NET Core中执行此操作的更新方法是使用TagHelpers
.
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro
基于这些示例(MaxLength,Label),您可以扩展现有的TagHelper
以满足您的需求.
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System.Linq;
namespace ProjectName.TagHelpers
{
[HtmlTargetElement("input", Attributes = "asp-for")]
public class RequiredTagHelper : TagHelper
{
public override int Order
{
get { return int.MaxValue; }
}
[HtmlAttributeName("asp-for")]
public ModelExpression For { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
if (context.AllAttributes["required"] == null)
{
var isRequired = For.ModelExplorer.Metadata.ValidatorMetadata.Any(a => a is RequiredAttribute);
if (isRequired)
{
var requiredAttribute = new TagHelperAttribute("required");
output.Attributes.Add(requiredAttribute);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要添加它以在视图中使用:
@using ProjectName
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper "*, ProjectName"
Run Code Online (Sandbox Code Playgroud)
鉴于以下模型:
using System;
using System.ComponentModel.DataAnnotations;
namespace ProjectName.Models
{
public class Foo
{
public int Id { get; set; }
[Required]
[Display(Name = "Full Name")]
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
和视图(片段):
<label asp-for="Name"></label>
<input asp-for="Name"/>
Run Code Online (Sandbox Code Playgroud)
将导致此HTML:
<label for="Name">Full Name</label>
<input required type="text" data-val="true" data-val-required="The Full Name field is required." id="Name" name="Name" value=""/>
Run Code Online (Sandbox Code Playgroud)
我希望这对任何有相同问题但使用.NET Core的人都有帮助.
归档时间: |
|
查看次数: |
115760 次 |
最近记录: |