HTML5似乎支持input fields的新things such as范围:
有没有人为ASP.NET MVC实现生成这些的HtmlHelper
扩展方法呢?可以使用接受htmlAttributes
的重载来执行此操作,例如:
Html.TextBoxFor(model => model.Foo, new { type="number", min="0", max="100" })
但那并不像以下那样好(或类型安全):
Html.NumericInputFor(model => model.Foo, min:0, max:100)
答案 0 :(得分:51)
通过使用DataType
属性,现在很多这些内容现已合并到MVC4中。
从this work item开始,你可以使用:
public class MyModel
{
// Becomes <input type="number" ... >
public int ID { get; set; }
// Becomes <input type="url" ... >
[DataType(DataType.Url)]
public string WebSite { get; set; }
// Becomes <input type="email" ... >
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// Becomes <input type="tel" ... >
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
// Becomes <input type="datetime" ... >
[DataType(DataType.DateTime)]
public DateTime DateTime { get; set; }
// Becomes <input type="date" ... >
[DataType(DataType.Date)]
public DateTime Date { get; set; }
// Becomes <input type="time" ... >
[DataType(DataType.Time)]
public DateTime Time { get; set; }
}
答案 1 :(得分:23)
答案 2 :(得分:11)
我不喜欢 DataTypes 属性是你必须在视图中使用 EditorFor 。然后,您无法使用 htmlAttributes 来装饰您的代码。还有其他解决方案,但我更喜欢这种方式。
在这个例子中,我只扩展了我最常使用的签名。
所以在课堂上:
using System.Linq.Expressions;
namespace System.Web.Mvc.Html
{
public static class HtmlExtensions
{
public static MvcHtmlString EmailFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Object htmlAttributes)
{
MvcHtmlString emailfor = html.TextBoxFor(expression, htmlAttributes);
return new MvcHtmlString(emailfor.ToHtmlString().Replace("type=\"text\"", "type=\"email\""));
}
}
}
如你所见,我刚刚为type =“email”更改了type =“text”,然后我可以在我的视图中使用:
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.EmailFor(m => m.Email, new { @class = "form-control", placeholder = "Email" })
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
html源代码:
<div class="form-group">
<label class="col-lg-2 control-label" for="Email">Email</label>
<div class="col-lg-10">
<input class="form-control" data-val="true" data-val-required="The Email field is required." id="Email" name="Email" placeholder="Email" type="email" value="" />
<span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
</div>
答案 3 :(得分:11)
最简单的方法是简单地添加type =“Email”作为html属性。它会覆盖默认的type =“text”。这是一个带有html5必需验证器的示例:
@Html.TextBox("txtEmail", "", new {placeholder = "email...", @type="email", @required = ""})
答案 4 :(得分:9)
喜欢它,什么时候可以驾驶这种类型的东西离开模特!!我用[DataType(DataType.PhoneNumber)]
装饰了我的模型,除了一个以外的所有模型都有效。
我意识到@Html.TextBoxFor
没有呈现type="<HTML5 type>"
但@Html.EditorFor
的做法很难。有道理我现在想起来我想到了,但发布这个可能会让其他人失去我刚刚失去的令人沮丧的几分钟;)
答案 5 :(得分:2)
我发现自己想要从HtmlHelper使用<input type='number' />
时获得的数字微调器,并最终解决了我自己。
与上面的RPAlbert的Html.Email For回答类似,我开始使用普通的Html.TextBoxFor,但后来我使用LinqToXml来修改HTML,而不仅仅是使用字符串替换。
从Html.TextBoxFor开始的优点是你可以使用MVC为你做的所有客户端验证。在这种情况下,我使用data-val-range
属性中的值来设置约束微调器所需的最小/最大属性。
public static HtmlString SpinnerFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
XDocument _xml = XDocument.Parse(html.TextBoxFor(expression, htmlAttributes).ToString());
XElement _element = _xml.Element("input");
if (_element != null)
{
_element.SetAttributeValue("type", "number");
if (_element.Attribute("data-val-range-max") != null)
_element.SetAttributeValue("max", _element.Attribute("data-val-range-max").Value);
if (_element.Attribute("data-val-range-min") != null)
_element.SetAttributeValue("min", _element.Attribute("data-val-range-min").Value);
}
return new HtmlString(_xml.ToString());
}
然后您可以像使用视图中的任何其他HtmlHelper一样使用它:
@Html.SpinnerFor(model => model.SomeNumber, new { htmlAttribute1 = "SomeValue" })
无论如何,这是我的实施,从你的问题我可以看到你想要的:
@Html.NumericInputFor(model => model.Foo, min:0, max:100)
调整我的方法来执行此操作非常简单,如下所示:
public static HtmlString NumericInputFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, int min, int max)
{
XDocument _xml = XDocument.Parse(html.TextBoxFor(expression, htmlAttributes).ToString());
XElement _element = _xml.Element("input");
if (_element != null)
{
_element.SetAttributeValue("type", "number");
_element.SetAttributeValue("min", min);
_element.SetAttributeValue("max", max);
}
return new HtmlString(_xml.ToString());
}
基本上我所做的就是重命名它并提供min / max作为参数,而不是从DataAnnotation属性中获取它们。
我希望有所帮助!