Click here to Skip to main content
15,884,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There's my Model, and i can't Edit it.

The errors on my ModelState returns:
XML
var errors = ModelState.Values.SelectMany(v => v.Errors);

[0] - "The Field Name is Required."
[1] - "The Field Id is Required."
[2] - "The Field Name is Required."

C#
public class Document
    {   
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Nome")]
        public string Name { get; set; }

        [Display(Name = "Descrição")]
        public string Description { get; set; }
                
        [Display(Name = "Arquivo")]
        public string Path { get; set; }

        [Required]        
        [Display(Name = "Categoria")]
        [Range(1, int.MaxValue, ErrorMessage = "Selecione uma categoria válida.")]
        public DocumentCategory Category { get; set; }
        
        [Display(Name="Privado")]
        public bool isPrivate { get; set; }
                
        public DateTime? InsertDate { get; set; }
        public DateTime? UpdateDate { get; set; }

        [Display(Name = "Playlist")]
        public int? PlaylistID { get; set; }

        [Display(Name = "Album")]
        public int? AlbumID { get; set; }

        public virtual Album Album { get; set; }
        public virtual Playlist Playlist { get; set; }     
        public virtual MyUser User { get; set; }
    }



Help me. Thanks.
Posted

1 solution

Uhhh, this may seem like a stupid response, but did you try supplying the required information it wants??

It appears as though you're trying to use entity models in the place of a view model.
 
Share this answer
 
Comments
EduChapow 17-Apr-14 8:09am    
Hello, i realy supplying all informations.
Because this is my Edit View, and they show all fields with the value in the database.
Dave Kreskowiak 17-Apr-14 13:49pm    
Yeah, that's your problem! Do NOT pass your entity model to the view. Even if you're passing all the data from the model, create and use a view model to pass between the view and your controller. This lets you put attributes on the viewmodel class different from those of the data model. The attributes you're using control access to the database. What's Required on the database may not be Required by the view. Get it?

Also, every field that you want to get back from the view must have a field in the view. If you want the viewmode to have, say, the ID of the record being edited, you have to put it in a Hidden field in your view page.

You will only get back the fields that are in the Form you create in the view page. Everything else will get a default value of null or 0.
Member 2112497 14-Mar-18 10:13am    
Yep - got it... and that's the reason you don't use Entity Objects.
Use CSLA or a similar framework instead.
EduChapow 20-Apr-14 1:28am    
I'm EF beginner, and i dont understand you.
I should be create one model to the entity and another model to the view?

thanks man
Dave Kreskowiak 20-Apr-14 9:11am    
That's what I've been saying, over and over.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900