开发者

four buttons and a bunch of conditions

I have an mvc app where I have 4 buttons on my web page. I need to enable/disable each button based on a workflow of a bunch of different conditions. What's the best way to manage this? Right now, there's almost 30 conditions and counting.

Not really looking for a specific solution 开发者_JS百科as much as a thought process or an approach to keep it simple.

Thanks, rod.


I would create different partial views for each scenario, not each button set.

This way, you can manage the views based on your business requirement.

In your main ViewModel, store the "scenario", not "flags for each button".

E.g:

public ActionResult Index()
{
    var viewModel = new ViewModel();
    viewModel.ScenarioName = "Scenario1";
    return View(viewModel);
}

Then if your "ScenarioName" matches the partial view name, you can simply do this:

<%: RenderPartial(Model.ScenarioName, Model.SomethingYouNeedForPartial) %>

That way your controller/viewmodel stays really clean.

Of course, this all depends on your "conditions" and business rules.


Without knowing too much about your situation (like whether these are known conditions, or if the conditions are based on actions by the user), this is where I would start:

I would start with a ViewModel:

public class ViewModel
{
    public bool ShowButton1 {get; set;}
    public bool ShowButton2 {get; set;}
    public bool ShowButton3 {get; set;}
    public bool ShowButton4 {get; set;}
}

And put the logic for the conditions in your controllers:

public ActionResult Index()
{
    var viewModel = new ViewModel();
    viewModel.ShowButton1 = condition1 && condition2;
    viewModel.ShowButton2 = condition3 && condition4;
}


WPF uses Commands, which allow you to assign a CanExecute event to each command. Heaps more info at http://msdn.microsoft.com/en-us/library/ms752308.aspx.

I'd avoid using a single large function with many nested conditionals, as these become impossible to read.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜