Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 - How do I properly switch views based on model context?

I'm putting together my first MVC application and have a question about how to properly accomplish my goal. Here's what I'm attempting...

One area in my application presents a list of batches of data, a list of Batch objects. For each Batch, a user can choose to process its data. The processing consists of a set of steps which I will present to the user in a wizard-like chain of views.

Each Batch has a type. Batch A of type L might consist of records of X, and Batch B of type M might consist of records of Y. The steps in the wizard are going to change slightly depending on the batch type. What I don't want to do is this (obviously poor code, just for illustration):

ProcessController

public ActionResult StepOne(int id)
{
    var batch = BatchRepository.Batch.SelectById(id);
    var records = BatchRepository.Data.SelectForBatch(batch);

    switch (batch.BatchType)
    {
        case "X":
            return View("TypeXStepOne", records);
        case "Y":
            return View("TypeYStepOne", records);
        default:
            return View();
    }

}

public ActionResult StepTwo(int id)
{
    var batch = BatchRepository.Batch.SelectById(id);
    var records = BatchRepository.Data.SelectForBatch(batch);

    switch (batch.BatchType)
    {
        case "X":
            return View("TypeXStepTwo", records);
        case "Y":
            return View("TypeYStepTwo", records);
        default:
            return View();
    }

}

...

And so on. This seems like a bad practice, and as my wizard might be 4 or 5 steps, and I currently have three different batch types, the amount of code in these actions becomes retardedly difficult to maintain.

What can MVC do for me in terms of managing this? I'd like to minimize the amount of work required if, for example, a fourth type was introduced.

I know that I'm probably going to have:

  • A partial view for each batch type and step in the wizard, there's really no way around that, though some partial views might get shared.

So, would it be better for each batch type to have a dedicated controller? I suppose I could then do something like this, where the batch type becomes the controller name.

BatchController

public ActionResult Process(int id)
{
    var batch = BatchRepository.Batch.SelectById(id);
    return RedirectToAction("StepOne", batch.BatchType, new { id = batch.Id });
}

But this would depend on the Batch type name matching the controller name, and I don't necessarily like that either. Update: Or, worst case, I have a single switch statement here with my three batch types, and then I can forward on to the appropriate controller.

So... ideas! I need 'em. And I'm no so far into this that I can't start something new, I'm just throwing ideas around in my head as an MVC newbie. MVC must have something that allows dynamic view switching based on context, whether it be by fancy routing techniques or a "ViewFactory" or some other trickery.

Any feedback whatsoever would be greatly appreciated!

And just for anyone wondering, I am stuck on the .NET 3.5 and IIS6, which is why I'm back on MVC 2 instead of 3.

UDPATE

Here's what I'm rolling with for now. I broke the BatchController into multiple controllers -- one for each batch type. Since they are mostly the same I created an abstract WizardController that has these methods:

public abstract ActionResult Step(int batchId, int stepNumber);

protected ViewResult ViewStep(int stepNumber, object model)
{
    return View(string.Format("Process\Step{0}", stepNumber), model);
}

And in my derived controller(s), something like:

public override ActionResult Step(int batchId, int stepNumber)
{
    var data = BatchRepository.BatchType.SelectByBatchId(batchId);

    return ViewStep(stepNumber, data);
}

It's quick and seems dirty but until I learn something better... thanks!

like image 416
Cᴏʀʏ Avatar asked Dec 01 '25 04:12

Cᴏʀʏ


1 Answers

Convention over configuration (the new hardcoding), something as simple as:

public ActionResult Step(int stepNo, int id)
{
    var batch = BatchRepository.Batch.SelectById(id);
    var records = BatchRepository.Data.SelectForBatch(batch);
    return View("Type"+batch.BatchType+"Step"+stepNo, records);    
}
like image 179
BlackTigerX Avatar answered Dec 04 '25 17:12

BlackTigerX