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:
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:
I would separate those in 2 different views and have the controller action pick the correct view based on the view model value (Children.Count in this case). But this being said the other approach is not wrong neither. It works fine for simple scenarios like this.
For this kind of a scenario typically you would have a Model property that actually was the flag of whether you can delete it or not (this is more in line with a ViewModel approach) so that the view isn't actually doing the logic, the controller is just telling the view what action is available.
@if(Model.CanDelete) {
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>
}
} else {
<p>You can't delete this!</p>
}
CanDelete can be filled in the controller using a combination of child data state, role membership, business status, etc. but to the view all of that stuff shouldn't matter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With