I'm trying to wrap my brain around the use of models in MVC. My model is set up differently than the database table it coordinates with because of how I need to use the model in the view. However, when the user submits the form from the view, potentially submitting changed values, I need to update the database with those new values. The problem I'm having is when I used a lambda expression, I cannot get the data types to match up with my table. Hopefully this will help explain more clearly what I mean:
Model
public class DataSharingModels
{
public string ReferenceID { get; set; }
public NBTC NBTCGroup { get; set; }
public Contractors ContractorsGroup { get; set; }
public Coordinators CoordinatorsGroup { get; set; }
public NGO NGOGroup { get; set; }
public Public PublicGroup { get; set; }
public SelectList FA_RA_List { get; set; }
}
public class NBTC
{
public Boolean NBTC_FA_Centroid { get; set; }
public Boolean NBTC_FA_Bound { get; set; }
public Boolean NBTC_RA_Centroid { get; set; }
public Boolean NBTC_RA_Bound { get; set; }
public Boolean NBTC_Spring_Sum { get; set; }
public Boolean NBTC_Spring_Analysis { get; set; }
public Boolean NBTC_Spring_Locate { get; set; }
public Boolean NBTC_Fall_Sum { get; set; }
public Boolean NBTC_Fall_Analysis { get; set; }
public Boolean NBTC_Fall_Locate { get; set; }
public Boolean NBTC_HabMon_Sum { get; set; }
public Boolean NBTC_HabMon_Analysis { get; set; }
public Boolean NBTC_HabMon_Locate { get; set; }
public Boolean NBTC_HabMgmt_Sum { get; set; }
public Boolean NBTC_HabMgmt_Analysis { get; set; }
public Boolean NBTC_HabMgmt_Locate { get; set; }
public Boolean NBTC_Inventory_Sum { get; set; }
public Boolean NBTC_OpSvy_Sum { get; set; }
public Boolean NBTC_OpSvy_Individ { get; set; }
}
//The NBTC class is essentially repeated four more times for Contractors,
// Coordinators, NGO, and Public. The prefixes are changed
//for the properties that make up those classes.
public class Contractors
{
public Boolean Contractors_FA_Centroid { get; set; }
public Boolean Contractors_FA_Bound { get; set; }
public Boolean Contractors_RA_Centroid { get; set; }
public Boolean Contractors_RA_Bound { get; set; }
public Boolean Contractors_Spring_Sum { get; set; }
public Boolean Contractors_Spring_Analysis { get; set; }
public Boolean Contractors_Spring_Locate { get; set; }
public Boolean Contractors_Fall_Sum { get; set; }
public Boolean Contractors_Fall_Analysis { get; set; }
public Boolean Contractors_Fall_Locate { get; set; }
public Boolean Contractors_HabMon_Sum { get; set; }
public Boolean Contractors_HabMon_Analysis { get; set; }
public Boolean Contractors_HabMon_Locate { get; set; }
public Boolean Contractors_HabMgmt_Sum { get; set; }
public Boolean Contractors_HabMgmt_Analysis { get; set; }
public Boolean Contractors_HabMgmt_Locate { get; set; }
public Boolean Contractors_Inventory_Sum { get; set; }
public Boolean Contractors_OpSvy_Sum { get; set; }
public Boolean Contractors_OpSvy_Individ { get; set; }
}
//And so on and so forth...
In the SQL database table, it is structured more like this:
PermissionID | FocalRefID | ShareGroup | StateID | CIP_FA_Centroid | CIP_FA_Boundary | etc...
1 | <guid> | NBTC | NE | Allowed | Allowed
2 |<same guid> |Contractors | NE | Not Allowed | Allowed
3 |<same guid> |Coordinators| NE | Not Allowed | Not Allowed
4 |<same guid> | NGO | NE | Allowed | Allowed
5 |<same guid> | Public | NE | Allowed | Not Allowed
Ignore that the guid is not a true guid in this table (it's not the primary key)... In the model, the ReferenceID
property will have the value that is represented in the FocalRefID
field of the table. The class NBTC
has all the attributes needed to fulfill the single record in the database with NBTC
in the ShareGroup
field (so the record with NBTC
ShareGroup would have under the CIP_FA_Centroid
field something that corresponds to the property NBTC_FA_Centroid
)
When I get to the controller to save changes to the database, I'm using a lambda expression like this (I would add more properties to make it only pull one record since multiple records have the same FocalRefID in the table as seen above):
NBTC nbtc = db.SharingPermissions.SingleOrDefault( NBTC => NBTC.FocalRefID == refID);
Unfortunately, that won't work because it can't convert type SharingPermission
(the database table) to type NBTC
Cannot implicitly convert type 'FocalAreaCounts.Models.SharingPermission' to 'FocalAreaCounts.Models.NBTC'
What I'm trying to do is pull the record with the same FocalRefID (and other properties) as what was sent back in the model and update it's values in the table. I think I'm just confused in how I'm using either my model or my lambda expression.
It was clear to me that I was returning the wrong type, I just didn't understand what the right type was. My model already has an NBTC class populated, that's what was sent back to the controller along with the other portions of the model, so I don't want to create a new variable of NBTC type. Since I want to pull a record from the database and store it in my variable, then the return type should be of the database class, in this case that's the SharingPermission class. New lambda expression:
SharingPermission nbtc = db.SharingPermissions.SingleOrDefault( SharingPermission => SharingPermission.FocalRefID == refID);