@Html.HiddenFor(model => model.Id)
的标准输出是
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="0" />
是否需要生成data-val-*
属性?它们看起来相当冗长,只是为了能够存储和返回下一个POST的数据而不需要。
禁用这些属性是个好主意吗?它们对某些场景有用吗?
ps:目前我可以暂时设置ViewContext.UnobtrusiveJavaScriptEnabled = false
使用这两个类:
public static class Extensions
{
public static NoUnobtrusiveJavaScript NoUnobtrusiveJavaScript(this HtmlHelper htmlHelper)
{
return new NoUnobtrusiveJavaScript(htmlHelper.ViewContext);
}
}
public class NoUnobtrusiveJavaScript: IDisposable
{
private bool _disposed;
private readonly bool _unobtrusiveJavaScript;
private readonly ViewContext _viewContext;
public NoUnobtrusiveJavaScript(ViewContext viewContext)
{
if (viewContext == null) {
throw new ArgumentNullException("viewContext");
}
_viewContext = viewContext;
_unobtrusiveJavaScript = viewContext.UnobtrusiveJavaScriptEnabled;
_viewContext.UnobtrusiveJavaScriptEnabled = false;
}
public void Dispose()
{
Dispose(true /* disposing */);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
// restore the original UnobtrusiveJavaScriptEnabled state
if (_viewContext != null)
{
_viewContext.UnobtrusiveJavaScriptEnabled = _unobtrusiveJavaScript;
}
}
}
public void EndForm()
{
Dispose(true);
}
}
*模式遵循FormExtensions.cs和MvcForm.cs的
答案 0 :(得分:1)
仅仅因为该字段是隐藏的,并不一定意味着您不需要验证。您可能正在通过javascript操纵隐藏字段,并希望保留内置验证,因此您不必自己动手。