Why isn't server side validation working in my ASP.Net MVC3 application with Entity Framework?

不想你离开。 提交于 2020-01-05 07:56:12

问题


I have an ASP.NET MVC3 application that uses entities generated from a database. Each entity has also has a separate partial class that uses the MetadataType attribute to associate each entity with a class that is decorated with a number of validation attributes (see below).

[MetadataType(typeof(Drawing.Metadata))]
public partial class Drawing
{
    private sealed class Metadata
    {
        [Required]
        [StringLength(50, MinimumLength = 3, ErrorMessage = "Drawing numbers must be between {2} and {1} characters in length.")]
        [DisplayName("Drawing number")]
        public string Number { get; set; }

        [Required]
        [StringLength(255, MinimumLength = 3, ErrorMessage = "Drawing titles must be between {2} and {1} characters in length.")]
        public string Title { get; set; }
    }
}

My controller code looks like this:

[HttpPost]
public ActionResult Create(Drawing drawing)
{
    if (ModelState.IsValid)
    {
        // Save to database here...
        return RedirectToAction("Index");
    }
    else
    {
        return View(drawing);
    }
}

I have used the Visual Studio templates to create the views to add, edit and delete the entities (The designer code has not been altered).

The problem I am having is that when I create an entity, validation only works if I have client side validation enabled. If I turn off the client side validation then ModelState.IsValid always seems to return true and returns me to the index page.

Can anyone provide any suggestions on how to get server side validation working with Entity Framework entities?

UPDATE:

It seems this question is similar to mine. The author of this post seems to have solved the problem but rather unhelpfully omitted to mention how they fixed the problem...


回答1:


I found another solution to this problem. Because I didn't really want to set my properties to nullable I added the following:

[DisplayFormat(ConvertEmptyStringToNull = false)]

Adding the following annotation to your model property fixes the error as well.




回答2:


After further investigation it seems that my problem is occuring due to a ConstraintException being thrown by my entity class (which inherits from ObjectContext) when the default model binder tries to bind the user input values (Null in this case) to the entity properties.

I can see 2 possible solutions to this:

  1. Relax the constraints on my database tables (I don't want to do this).
  2. Make the entity fields nullable (use the entity designer set the nullable property to yes)

I have used and tested the second option and can confirm that server side validation now works as expected.

Whilst researching solutions to this problem I have come to the conclusion that the problem is due to my entities inheriting from ObjectContext which is quite a heavy class. I found a lot of tutorials which used a code-first approach. In this case, the entity class will inherit from DbContext which is much more lightweight so I guess this could be considered a third solution to the problem.



来源:https://stackoverflow.com/questions/8092759/why-isnt-server-side-validation-working-in-my-asp-net-mvc3-application-with-ent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!