asp.net-mvc 不需要的属性不断获取 data-val-required 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4700172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unrequired property keeps getting data-val-required attribute
提问by frennky
This is the model with it's validation:
这是带有验证的模型:
[MetadataType(typeof(TagValidation))]
public partial class Tag
{
}
public class TagValidation
{
[Editable(false)]
[HiddenInput(DisplayValue = false)]
public int TagId { get; set; }
[Required]
[StringLength(20)]
[DataType(DataType.Text)]
public string Name { get; set; }
//...
}
Here is the view:
这是视图:
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Tag</legend>
<div>@Html.EditorForModel()</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
And here is what get's renderd:
这是 get 渲染的内容:
<form action="/Tag/Create" method="post">
<fieldset>
<legend>Tag</legend>
<div><input data-val="true" data-val-number="The field TagId must be a number." data-val-required="The TagId field is required." id="TagId" name="TagId" type="hidden" value="" />
<div class="editor-label"><label for="Name">Name</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-length="The field Name must be a string with a maximum length of 20." data-val-length-max="20" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span></div>
...
</fieldset>
</form>
The problem is that TagId validation gets generated althoug thare is no Required attribute set on TagId property. Because of that I can't even pass the client-side validation in order to create new Tag in db. What am I missing?
问题是 TagId 验证会生成,尽管 TagId 属性上没有设置必需的属性。因此,我什至无法通过客户端验证以在 db 中创建新标签。我错过了什么?
回答by frennky
I found the answer. Just add this to Application_Start
:
我找到了答案。只需将此添加到Application_Start
:
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
回答by Curtis Yallop
Make the view-model value-types nullable. Then they won't be Required by default.
使视图模型值类型可以为空。那么默认情况下它们不会是必需的。
Note also if you put the attribute 'required="false"' in html 5 (if you set html 5 in your doctype meta data), it will see "required" and make it required. You can use dojo-data-props="required:false".
另请注意,如果您将属性 'required="false"' 放在 html 5 中(如果您在 doctype 元数据中设置 html 5),它将看到“required”并使其成为必需。您可以使用 dojo-data-props="required:false"。
回答by nthpixel
frennky's solution only removed data-val-required
but in my case I still had data-val-number
and data-val
frennky 的解决方案仅被删除,data-val-required
但在我的情况下,我仍然有data-val-number
和data-val
I had to add the two lines below to Application_Start to get rid of everything.
我不得不将下面的两行添加到 Application_Start 以摆脱一切。
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
回答by Darin Dimitrov
The problem is that the value of the hidden field is empty. This shouldn't happen if you use integer type. I suppose that the TagId property is defined as a nullable type in the Tag
class. So either assign it a value before rendering the view or use an integer type:
问题是隐藏字段的值为空。如果您使用整数类型,则不应发生这种情况。我想 TagId 属性在Tag
类中被定义为可空类型。因此,要么在呈现视图之前为其分配一个值,要么使用整数类型:
[MetadataType(typeof(TagValidation))]
public partial class Tag
{
public int TagId { get; set; }
public string Name { get; set; }
}
so that the generated hidden field looks like this:
这样生成的隐藏字段如下所示:
<input
data-val="true"
data-val-number="The field TagId must be a number."
data-val-required="The TagId field is required."
id="TagId"
name="TagId"
type="hidden"
value="0"
/>
Also normally client side validation shouldn't be triggered for this hidden field.
此外,通常不应为此隐藏字段触发客户端验证。
回答by takepara
jquery validate target cheking "disabled" html attribute.
jquery 验证目标检查“已禁用” html 属性。
$(function () {
$("#TagId").attr("disabled", "disabled")
});
or use Nullable.
或使用可空。
hope this code!
希望这个代码!
回答by t2t
With MVC4 you can also use this:
使用 MVC4 你也可以使用这个:
@{ Html.EnableClientValidation(false); }
@Html.EditorForModel()
@{ Html.EnableClientValidation(true); }
回答by Jani Devang
Make your Model or View-Model property value-types "nullabel". This will solve your problem.One important thing that remove "required" attribute from your tag otherwise it will take i "required"
使您的模型或视图模型属性值类型“空标签”。这将解决您的问题。从您的标签中删除“必需”属性的一件重要事情,否则它将需要我“必需”
Example:-
例子:-
public class ViewModle
{
public int? foo{get;set;}
}
Here in example foo is integer nullable type, this will no longer required in mvc.
在示例中 foo 是整数可为空类型,这在 mvc 中不再需要。
Hope this will help you.
希望这会帮助你。