Conditionally displaying parts of my view

Say I have an asp.net mvc site with a page listing products. On this page, I have a “delete” button, which should only be displayed to the user who inserted the product. What is the best way to do this?

One of the ways I thought to do this was to set a boolean value in the controller to give an idea of ​​whether the button should be displayed. Sort of:

if(IsProductOwner(UserId))
    ViewData["CanDelete"] = true;

Then in the view, I can just do

<% if((boolean)ViewData["CanDelete"] == true) { %>
    // show delete button
<% } %>

But is there a better way to do this?

+3
source share
1 answer

My initial thought is that you should at least make this function of the Product class so that you can:

<% if (product.IsOwnedBy(UserId)) { %>
    // show delete button
<% } %>

floaty ViewData - , .

, , , .

+2

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


All Articles