This is a migrated thread and some comments may be shown as answers.

How to hide action in window depending on value

2 Answers 70 Views
Window
This is a migrated thread and some comments may be shown as answers.
Miguel
Top achievements
Rank 1
Miguel asked on 21 Feb 2019, 11:34 AM

Hi,

I'm my app I've a window with an action (a save button) defined. I'm trying to hide or not this action depending in a ViewData value.  If I've write permissions I should see the button, else I shouldn't. How could achieve it? I could create 2 different windows or control it with jquery and hidding the button, but I think it's not the best way.

My window:

@(Html.Kendo().Window()
              .Name("windowZonaExclusivas")
              .Width(930)
              .Resizable()
              .Modal(true)
              .Visible(false)
              .Title("TEst")
              .Draggable(true)
              .Actions(actions => actions.Custom("Save").Maximize().Close())
              .LoadContentFrom("EditZona", "ZonasExclusivas", new { idZona = -1 })
)

Any help will be appreciated.

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 22 Feb 2019, 02:31 PM
Hello Miguel,

I'd suggest you add conditional logic (an if-block) in the cshtml of the view you load in the Window, that will use the ViewData flag and render the button only if necessary. Here's a basic example:

    //controller
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["HasPermissions"] = false;//use actual business logic here
            return View();
        }
    }
 
//view
@if((ViewData["HasPermissions"] as bool?) == true)
{
    <input type="submit" value="save" />
}

Another approach is to redirect the user without rights to a different view that does not have a form and inputs, but only read-only data (this is perhaps the safer approach as a form can be submitted with JS without a submit button). Here's a basic example:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        bool HasPermissions = false;//use actual business logic here
        if (!HasPermissions)
        {
            return View("ReadOnlyDetailsViewWithoutForm");//render a different view that does not have the inputs at all
        }
        return View();
    }
}


Regards,
Marin Bratanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Miguel
Top achievements
Rank 1
answered on 25 Feb 2019, 08:05 AM

Hi, thanks for your reply. At last I've achieve it in a different and simple way:

 

@(Html.Kendo().Window()
    .Name("windowZonaExclusivas")
    .Width(930)
    .Resizable()
    .Modal(true)
    .Visible(false)
    .Title("TEst")
    .Draggable(true)
    .Actions(actions =>
    {
        if ((bool)ViewData["canAdd"] || (bool)ViewData["canEdit"]) actions.Custom("Save");
        actions.Maximize().Close();
    })
    .LoadContentFrom("EditZona", "ZonasExclusivas", new { idZona = -1 })
)
Tags
Window
Asked by
Miguel
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Miguel
Top achievements
Rank 1
Share this question
or