字段 ID 是必需的。尝试通过 MVC3 添加新对象时出错,ModelStat.IsValid false 错误?

发布于 2024-12-18 01:32:19 字数 2374 浏览 2 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-12-25 01:32:19

这是因为从表单传入的数据没有价值。验证引擎检查值字典,如果未找到,则添加 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.

尤怨 2024-12-25 01:32:19

我也有同样的问题。我将主键设置为可为空类型,一切都非常顺利。

I had the same problem. I set my Primary key to nullable type and everything worked like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文