C# - Retrieve data from persistence storage and save it to the view model

前端 未结 1 1781
生来不讨喜
生来不讨喜 2021-01-26 08:43

Hello I have a controller method that I want to return the view model of that looks like this

This is what it would look like if it was hard-coded

 public          


        
相关标签:
1条回答
  • 2021-01-26 09:20

    Hmmm I thought I answered this question before: https://stackoverflow.com/a/62782124/2410655. Basically you can't have a for loop like that in the middle of the view model.

    I would like to add 2 more things to it.

    1. Id?

    If the special order summary expects an ID, don't declare it as optional. If you do so, you have to add more logic to check whether there is an ID or not.

    If the order summary expects an ID, just declare it as int id. And if the client doesn't provide it, let the MVC framework handle the error. Now depending on your setup, your MVC might throw a 404, or 500, or a user-friendly page. It's up to you, the developer, to set it up.

    2. Be careful on NullReference Exception

    In your code example, I see you used FirstOrDefault() on the item instance. That will bite you if it comes back as NULL and you call db.Items.Find(ii.ItemID)...


    So based on your example, I would change the code to:

    public ActionResult SpecialOrderSummary(int id)
    {
        JObOrder jobOrder = db.JobOrders.Find(id);
        if (jobOrder == null)
        {
            return HttpNotFound();
        }
    
        ItemInstance itemInstance = db.ItemInstances
            .Where(x => x.serialNumber == jobOrder.serialNumber)
            .FirstOrDefault();
    
        Item item = null;
        if (itemInstance != null)
        {
            item = db.Items.Find(itemInstance.ItemID);
        }
    
        var vm = new JobOrderSummaryViewModel
        {
            JobOrderId = jobOrder.ID,
            Parts = new List<ItemPartViewModel>();
        };
    
        if (item != null)
        {
            vm.ItemId = item.ItemId;
            vm.ItemName = item.ItemName;
    
            foreach(ItemHasParts ihp in item.IHP) 
            {
                // Does Part and Item have many-to-many relationships?
                // If so, you might be able to get the part by
                // ihp.Part instead of looking it up using the ID.
                // Again, it depends on your setup.
                Part part = db.Parts.Find(ihp.PartID);
    
                if (part != null)
                {
                    vm.Parts.Add(new ItemPartViewModel
                    {
                        PartId = part.ID,
                        PartName = part.Name
                    });
                }
            }
        }
    
        return View(vm);
    }
    

    Note:

    You have additional calls back to the database inside the loop (db.Parts.Find(ihp.PartID);). That will cause performance issue if you have huge data. Is there any way you can fetch all your data you needed once at the beginning?

    0 讨论(0)
提交回复
热议问题