2

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.

Community
  • 1
  • 1
JP13
  • 109
  • 8
  • Are you rendering new pages for the search and next retrieval? If you are, database calls are fully justified. – Robert Harvey Apr 29 '19 at 18:19
  • In any case, there's no "correct way" without context. If speed is of paramount importance, I would consider using `TempData` even though I dislike storing more than an ID there. If accuracy and up-to-date data is more important than speed, always go with the database call. – Robert Harvey Apr 29 '19 at 18:21
  • You can store complex objects in TempData. It's not available out of the box. Have a look at this [answer](https://stackoverflow.com/a/35042391/5243697) – Shahzad Hassan Apr 29 '19 at 18:31
  • @RobertHarvey Yes we are rendering new pages for the search and retrieval. If the user had any existing information on the master create I am saving that in TempData to repopulate on return. – JP13 Apr 29 '19 at 19:15
  • @ShahzadHassan I did see that answer, I wasn't sure where to add it though. It was also a few years ago so I wasn't sure it was still current. Where would I add those methods? – JP13 Apr 29 '19 at 19:15
  • In a static class, just as shown in the answer. The class name can be anything, however, should be meaningful. As those methods are an extension to the TempData, the class name is `TempDataExtensions`. You can create this class anywhere in the project. Then import the namespace of the class, where you need to use those methods, and they will be available to you for TempData. If you are not familiar with the Extionsion Methods, please have a look at the official [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) – Shahzad Hassan Apr 29 '19 at 20:29
  • I just tried looking at the documentation. However, the [solution](https://stackoverflow.com/a/35042391/5243697) is still valid. Also mentioned at GitHub [here](https://github.com/aspnet/AspNetCore/issues/4816). It may become available out of the box in .Net Core 3.0 – Shahzad Hassan Apr 29 '19 at 20:51

0 Answers0