Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two views and both need to be strongly type my model class is having 8 columns in total and my first view needed all these ten fields so i put Required validator on all fields , but on another view i just need the first 4 fields and the rest i do not know , so now I am using the same model but while saving the required the entity framework says that there are validation error as i am not providing the rest of the four fields for second view . in that case i even tried the bind[(exclude="")] but it did not work for me .

https://i.postimg.cc/MpXKnhMD/modelerror.jpg[^]

What I have tried:

When i tried ModelState.Remove("Password") and other fields before saving the record it works for all fields but not working for password field only .
ModelState.Remove("Password"); does not work while saving the record it still gives error like "{"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."} When i drildown i found it shows error on Password field althoguh before saving i am excluding the Password field using ModelState.Remove("Password");
Even the Exclude property also did not work
Posted
Updated 17-Oct-19 22:44pm

1 solution

Have a model that only implements the 4 fields needed for the second view, then create a model that inherits from that model and has the other fields for the first view. Then it is just a matter of passing the right model to the right view.

class SmallView
{
    // common four properties
}

class LargeView : SmallView
}
    // properties only on the large view
}
 
Share this answer
 
v2
Comments
Malikdanish 18-Oct-19 4:47am    
should i not create a small view with just four properties and then use that view but how do i map this view to the same table to which the first view is mapped
F-ES Sitecore 18-Oct-19 5:00am    
Yeah, you'll probably need two different views otherwise you have a view that deals with properties that don't exist or aren't relevant.
Malikdanish 18-Oct-19 5:04am    
F_ES siteCore
ModelState.Remove("FirstName");
ModelState.Remove("LastName");
ModelState.Remove("ReportingManager");
ModelState.Remove("ResetPasswordCode");
ModelState.Remove("Mobile");
ModelState.Remove("Password");
why it is not removing Password from model
Malikdanish 18-Oct-19 5:22am    
Now getting this error messsage
The entity type ActivateUser is not part of the model for the current context..
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The entity type ActivateUser is not part of the model for the current context.

Source Error:


Line 162: // ModelState["Password"].Errors.Clear();
Line 163: // registration.Password = "ASDF";
Line 164: db.Entry(ActivateUsers).State = EntityState.Modified;
Line 165: try
Line 166: {
F-ES Sitecore 18-Oct-19 5:28am    
If these are automatically generated Entity Framework classes then you'll probably struggle to make them inherited. You're probably better leaving the EF classes as they were and creating new classes to use just as your view models rather than passing the EF classes to your views.

It will need a bit more leg work as you'll need to get your data from the context

var efData = context.MyTable.Where(....);

then create your view model class and copy the properties over

var model = new MyView();
model.Prop1 = efData.Prop1;
model.Prop2 = efData.Prop2;

So you'll have your EF class that represents the data, then you will have a view model class specific to each view and you copy the data between them as needed.

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