I understand that a view should only display stuff and do no logic itself outside of what is required to display information.
What is the best way, in keeping that in mind, to handle this kind of simple scenario:
- User clicks Delete Item
- If the item is still associated with others, show “you can’t delete this.”
- Else, show a confirm form that posts to action /Delete/Id
I could very easily in the view do something like:
@if (Model.Children.Count > 0)
{
<p>
You can't delete this!
</p>
}
else
{
using (Html.BeginForm())
{
<p>
Are you really sure you want to delete this?
</p>
<p>
<input type="submit" value="Confirm" /> |
@Html.ActionLink("Cancel", "Index")
</p>
}
}
Is there a compelling reason to make TWO Views and have the controller return the appropriate view depending on how many children there are? Seems like a tradeoff of simplicity and separation of concerns.
It’s a pretty simple example, so at first glance it appears harmless (and if it stays that way it certainly is). However, keep these factors in mind: