0

I'm working on a simple web app with 2 Models - Company & Customer. I've created a View Model for these 2 classes that passes both their data to the view. My next step is to configure the controller so the user can edit both models from the same view.

I've modified the Index() + Edit() methods (both have the same code)

public async Task<IActionResult> Index(int? id)
{
    var viewModel = new BusinessViewModel();
    viewModel.Companies = await _context.Companies
          .AsNoTracking()
          .ToListAsync();

    viewModel.Customers = await _context.Customers
        .AsNoTracking()
        .ToListAsync();

    if (id != null)
    {
        ViewData["CompanyID"] = id.Value;
        Company Company = viewModel.Companies.Where(
            i => i.ID == id.Value).Single();

        ViewData["CustomerID"] = id.Value;
        Customer Customer = viewModel.Customers.Where(
            r => r.CustomerID == id.Value).Single();
    }

    return View(viewModel);
}

Where I struggle with is updating the EditPost method. How do I configure it so that when the user clicks on "save" both the Company & Customer data are updated & displayed on the view?

Let me know if you need more info. Thanks!

Vy Nguyen
  • 57
  • 1
  • 10

1 Answers1

0

Not to be rude but you do not understand the Model View Controller (MVC) pattern and thus it is causing your code to be complex and confusing and now you are getting stuck.

Following, not even strictly, the MVC software design pattern, you should have a CompanyController for Company operations and a CustomerController for Customer operations. And so it follows, you have Views for Customer operations and Views for Company operations.

I think your confusion is your are trying to give too little code too much responsibility if that makes sense.

You should look at a simple scaffolded MVC app. I believe there may still be a scaffolding ASP.NET MVC template in Visual Studio 2015...

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • No offense taken. Yes, I am writing my first web app. I don't really have a good grasp of the MVC pattern. I guess I need to study harder. – Vy Nguyen Apr 25 '17 at 22:43