MVC + EntityFramework + field validation

Let me start talking about the problem: when I submit my view, my action receives an object from the Customer class that has foreign keys that are integer but must be null.

The class was generated by EF Designer and my database structure is as follows (I want to put an image, but it tells me I need 10 rep):

Customer (Table)
  CityId -> City (Table)
            CityId
            StateId -> State (Table)
                       StateId
                       CountryId -> Country (Table)
                                    CountryId

      

Renewal, I have a Client who needs to be in a city that needs to be associated with a state that needs to be associated with a country.

In my other hand, I have a View that has 3 Dropdowns: 1 to select a country, the other to select one state from the selected country, and the third to select a city from the selected state. When I select Country, View (via JSON) populates the other dropdown with states, etc., so obviously I'm only storing the CityId.

The problem is when I try to submit MVC shows me the following check:

The CountryId field is required.
The StateId field is required.
The CityId field is required.

      

This is because these fields are Int32. So, the first thing I did was change these fields to nullable in EF Designer (because I want to put a personal validation with ModelState.AddModelError ).

I also changed the Multiplicity to 0..1.

In the database, these fields must be invalid.

But now I am getting the following error: Error 3 Error 3031: Problem displaying fragments starting at line 1074: Column with invalid Cidades.EstId values ​​in Cidades table maps to nullable entity property.

What's the best way to fix this?

Thank you in advance

+3


source to share


2 answers


The problem is easy to solve: design the view so that the published form contains only the properties of the object you are trying to store in the database ( Customer

).

It means that:

  • you should only use Html Helpers to create controls that refer to properties Customer

    , not properties of other objects. That is, don't use Html Helpers to create controls for "Customer.City.CityId" but for "Customer.CityId". This way, you ensure that only properties are sent to your controller action Customer

    .
  • (I repeat myself, but) You should avoid creating controls associated with any other related objects: city, state and country. These controls must be regular HTML objects. You can access them later using JavaScript (for example, to load cascading lists). The only control that should be associated with any property is a dropdown ( <select>

    ), which contains the city that should be named Customer.CityId

    , not Customer.City.CityId

    .


If you do this, when you post your form, you are only posting the object Customer

, so the post action in your controller should only take a parameter Customer

. Then EF will not perform any checks on any of the other objects, simply because they don't exist, and you will get rid of the problem.

About validation: depending on your configuration, validation can happen on both the client and the server. If you take these precautions, you will avoid problems on both sides. Don't change your model to accept zeros! This is a bad hack!

NOTE. Since you didn't show your code, I'm guessing what it looks like more or less. If my answer is not clear enough, please update your question and give me a comment with @JotaBe

+1


source


I found that removing the primary check imposed by MVC:

ModelState.Remove("City.State.CountryId");
ModelState.Remove("City.StateId");
ModelState.Remove("CityId");

      



But the hint as given by @JotaBe is an interesting solution too.

0


source







All Articles