Question

This came up during one of our retrospectives and wanted some additional feedback and a spot check. We currently have a number of views that enable/disable based on boolean flags (Model.IsNew is an example). I'm of the opinion that views should be as simple as possible and controllers should determine the data for that view, not necessarily how it works. I think views, partial or full, should be -told- what to do and handle that vs the view determining what should be shown/hidden. A very basic example goes as follows but covers both sides of this and is mostly a reflection of what we have ...

A controller has a pair of methods (post/get) called Details. [Get]Details has a single parameter, Id and [Post]Details takes id and a viewmodel. Within the post, the method is ~30 lines long checking for valid model, determining if its new, if a certain value changed (triggers redirect) and so on (I think this is incorrect). The [Get]Details check for empty id, populates the necessary dropdowns, nothing fancy (I think this is right). The detail view itself contains a small bit of logic : If (!Model.IsNew) { RenderAction(History => History.Show(id); } (I think this within an if is incorrect, the Show should know what to display, regardless if its new). The plus to this is the layout for said Detail view isn't done twice. Details/Add would be nearly identical, minus some disabled fields depending on state (maybe these should be partials?) -- the entity could be disabled/deleted making the values editable or not.

Thoughts, opinions, insights?

Was it helpful?

Solution

why not create multiple Views and Actions?

Details

Edit
[HttpPost]
Edit

Create
[HttpPost]
Create

Then they could share a Model object

public ThingModel
{
    public Thing Thing { get; set; }
}

or have the Create and Edit actions use a safer (prevent html injection) model which will also let you use the Validation options built in.

public ThingEditorModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsNew { get { return Id == 0; } }
}

And then for Edit and Create you can create an EditorTemplate (Shared/EditorTemplates/ThingEditor.ascx) that the Create and Edit could share

OTHER TIPS

I think you're on the right track...use the 'main' view for layout and then use Templated Helpers for the 'logic'. Lean on Html.DisplayFor(x=>x.Thing) and EditorFor

You definately don't want the layout in two places.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top