How to hide action in window depending on value

3 posts, 0 answers
  1. Miguel
    Miguel avatar
    17 posts
    Member since:
    Jan 2019

    Posted 21 Feb 2019 Link to this post

    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. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5609 posts

    Posted 22 Feb 2019 Link to this post

    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.
  3. Miguel
    Miguel avatar
    17 posts
    Member since:
    Jan 2019

    Posted 25 Feb 2019 in reply to Marin Bratanov Link to this post

    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 })
    )
Back to Top