How should “shared” be visible?

I understand that a presentation should only display material and not do any logic beyond what is required to display information.

What is the best way, bearing this in mind, to deal with such a simple scenario:

  • User clicks Delete item
  • If the item is still linked to others, show "you cannot delete this."
  • Else, show the confirmation form in which the actions / Delete / Id will be indicated

I could very easily see 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 good reason to make TWO Views and return the appropriate representation to the controller depending on the number of children? It seems like a trade-off between simplicity and separation of concerns.

+6
source share
3 answers

This is a fairly simple example, so at first glance it seems harmless (and if it remains the way it is). However, be aware of these factors:

  • What if it becomes more than just kids? Perhaps over time there will be three other relationships, and now you need to check them all in your opinion? Suddenly the code smell is much stronger.
  • The inclusion of this type of logic in the controller may make other approaches to the problem more obvious or easier later, for example, adding an ajax version that will allow you to give the user “you cannot delete this” feedback without leaving the previous page.
+4
source

I would separate them from two different views and so that the controller action selects the correct view based on the value of the view model (in this case, Children.Count). But, as they say, another approach is also not mistaken. It is great for simple scripts like this.

+3
source

For this type of scenario, you usually have the Model property, which was actually a flag of whether it can be deleted or not (this is more like the ViewModel approach), so that the view does not actually execute logic, the controller simply reports which 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 populated by the controller using a combination of child data status, role membership, business status, etc., but all of this should not matter for presentation

+2
source

Source: https://habr.com/ru/post/900305/


All Articles