I am still learning asp.net core and need to know if I'm going about this problem the correct way. Within my app I have a page that is acting as a wizard to create a complex object. I am using a view model to control the inputs. A brief overview of the viewmodel is:
Master Object
-2 Child Objects (that contain 3 child objects each)
-Second Child Object (that contains 1 child object)
I have gotten to the point where I can create the Master Object and all children without any issues. The next step is to add a search option for the 2 child objects.
Currently I have a link to a search page for the child object, I am using TempData to pass the ID of the selected object back (since TempData doesn't support complex objects). On my originating controller I am checking the TempData for the key, and if it exists I am querying the database (via _context) for the selected object.
Child Controller:
TempData["ChildObjectId"] = SelectedID;
return Redirect(MasterObjectControllerCreationURL);
Master Object Controller:
if (TempData.ContainsKey("ChildObjectId"))
{
ViewData["ChildObject"] = _context.ChildObject.Include(x => x.SubObject).Where(x => x.ChildObjectId == Convert.ToInt32(TempData["ChildObjectId"])).FirstOrDefault();
TempData.Remove("ChildObjectId");
}
Master Object Create Page:
if (ViewData.ContainsKey("ChildObject"))
{
Set field variables for the fields we want.
}
Ultimately this is resulting in 2 database calls, one for the search, and then one for the next retrieval. Is this the most efficient way to handle the problem, or is there a better solution? It seems like there should be a more efficient way to handle this. I know I could just set TempData for all of the fields in question, but that would result in 40 possible fields being stored in TempData. Maybe this isn't a problem and is the correct way, I just don't know enough about ASP.Net Core and MVC to know.