字段 ID 是必需的。尝试通过 MVC3 添加新对象时出错,ModelStat.IsValid false 错误?
我有这个 ViewModel:
[Key]
public long KlijentID { get; set; }
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
[StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
public string ImePrezime { get; set; }
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
[StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
public string Adresa { get; set; }
//Rest of the Class, not important for the question.
在视图中我有:
@using (Html.BeginForm("Edit", "Klijenti"))
{
@Html.HiddenFor(model => model.KlijentID)
<div class="editor-label">
@Html.LabelFor(model => model.ImePrezime)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ImePrezime)
@Html.ValidationMessageFor(model => model.ImePrezime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Adresa)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Adresa)
@Html.ValidationMessageFor(model => model.Adresa)
</div>
<p><input type="submit" value="Spremi" /><p>
}
当我使用它从数据库更新对象时,控制器操作工作正常:
if (!ModelState.IsValid) throw new ValidationException();
var k=new Klijent();
Mapper.Map(klijent, k);
repo.SaveKlijent(k);
TempData["msg"] = MyResources.Properties.Resources.SaveDone;
return RedirectToAction("Index", page);
但是当我尝试添加新对象时,ModelState.IsValid
失败说需要 KlijentID。 ErrorMessage:“KlijentID 字段是必需的。”
我已经检查过,它被设置为 0,因为它应该是新对象的值。这里有什么问题呢?
更新: 我尝试将其添加到 Global.asax 中的 Application_Start
中,
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
结果是我仍然收到验证错误,现在它显示: ErrorMessage:“需要一个值。”
这有点奇怪,它似乎非常想要该值。机器里有鬼?
I have this ViewModel:
[Key]
public long KlijentID { get; set; }
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
[StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
public string ImePrezime { get; set; }
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
[StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
public string Adresa { get; set; }
//Rest of the Class, not important for the question.
In the view i have:
@using (Html.BeginForm("Edit", "Klijenti"))
{
@Html.HiddenFor(model => model.KlijentID)
<div class="editor-label">
@Html.LabelFor(model => model.ImePrezime)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ImePrezime)
@Html.ValidationMessageFor(model => model.ImePrezime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Adresa)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Adresa)
@Html.ValidationMessageFor(model => model.Adresa)
</div>
<p><input type="submit" value="Spremi" /><p>
}
When i use it to update an object from database the controller action works fine:
if (!ModelState.IsValid) throw new ValidationException();
var k=new Klijent();
Mapper.Map(klijent, k);
repo.SaveKlijent(k);
TempData["msg"] = MyResources.Properties.Resources.SaveDone;
return RedirectToAction("Index", page);
But when i try to add a new object, than the ModelState.IsValid
fails saying that the KlijentID is required.ErrorMessage:"The KlijentID field is required."
I have checked, it is set as 0 as it should be for a new object. What is the problem here?
UPDATE:
I have tried to add this to my Application_Start
in Global.asax
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
The result is i still get a validation error, just now it says:ErrorMessage: "A value is required."
This is getting a bit strange, it just seems to want that value really bad. Ghosts in the machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为从表单传入的数据没有价值。验证引擎检查值字典,如果未找到,则添加 ModelState 错误。值等于0,因为它是int类型的默认值。
要解决这个问题,只需在 Create 视图中添加 @Html.HiddenFor(model=>model.Id) 即可。
This is because there was no value in incoming data from form. Validation engine checks dictionary of values, and if it is not found, adds ModelState error. Value is equal to 0 because it is default value of int type.
To solve this thing, just add @Html.HiddenFor(model=>model.Id) in your Create view.
我也有同样的问题。我将主键设置为可为空类型,一切都非常顺利。
I had the same problem. I set my Primary key to nullable type and everything worked like a charm.