这是验证的模型:
[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; }
//...
}
Run Code Online (Sandbox Code Playgroud)
这是观点:
<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>
Run Code Online (Sandbox Code Playgroud)
这是得到的渲染:
<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 …
Run Code Online (Sandbox Code Playgroud) 我有这个字段,由于某种原因,当我点击提交时,获得一个验证消息,该字段是必需的.
[DisplayName("Total Budget:")]
public double Budget { get; set; }
@Html.EditorFor(model => model.account.Budget)
@Html.ValidationMessageFor(model => model.account.Budget)
public class Account
{
[DisplayName("Total Budget:")]
public double Budget { get; set; } //dropdown
}
Run Code Online (Sandbox Code Playgroud) 是否有[Bind(Exclude = "Id")]
(相关问题)的替代方案?
我能写一个模型活页夹吗?
我有一个像这样的模特
public int Id {get;set;}
[Required]
public string FirstName{get; set}
[Required]
public string LastName{get; set}
Run Code Online (Sandbox Code Playgroud)
Id在DB中自动生成.当我想要调用Create Action时ModelState说"需要Id字段"!!!! 我找到了这个问题,但它不是干净的解决方案.还有其他方法可以解决这个问题吗?
有没有办法改变值类型的Mvc ModelBinder行为?