Base solution for your next web application
Open Closed

Angular js .cshtml view / calling ApplicationService code #2580


User avatar
0
bilalhaidar created

Hi,

In one of the forms I am working on. I need to show/hide fields. The data to show/hide fields is stored in the database, so for example, Field 1 - Show, Field 2 - Hide.

So in the cshtml view (Angular view) I need to load this info from the database. Is this doable?

The reason I am asking is because this .cshtml view is used by Angular not a normal MVC View that I can pass a Model to it.

Thanks


21 Answer(s)
  • User Avatar
    0
    bilalhaidar created

    Do you think I can inherit from this class: AbpWebViewPage

    then add my code there? In my case, the things I want to read from DB are specific for a View, so is it possible for that View only, to use a class that inherits from AbpWebViewPage and that read some data from DB to use on the View?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I think it is better to load data in your angularjs controller and make bindings according to your Show/Hide values. Angular will not render html of hidden parts.

  • User Avatar
    0
    bilalhaidar created

    So I do it on the client side not server side?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you don't want to send data to client as well (I think in your case you don't want to), then you need to do it in your app service method on server side.

    I didn't understand the case in the first read, sorry.

  • User Avatar
    0
    bilalhaidar created

    OK, if I create that on AppService layer, how would I access the info on Razor views given the fact I am loading Razor views in Angular?

    Shall I extend the Views Base class with methods like @IfShown("FieldName") {} ?

    You got my point? A Razor View is requested, then inside the View I need to check what fields are to be shown/hidden and render accordigly.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, you can create a helper method like @IfShown in your case. It seems reasonable.

  • User Avatar
    0
    bilalhaidar created

    Where to place it? In the base class of the Views?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you are going to need it in most of the views, you can place it in the base view.

  • User Avatar
    0
    bilalhaidar created

    I will be using it in one View, so where do you think best to add it? On the View itself?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I think it is better to define a seperate class and use it in your view even if it is a single place.

  • User Avatar
    0
    bilalhaidar created

    So, I would define an IDataLoader and DataLoader and then in my view, I Resolve this class and call that method on it? Or something else?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you want to use this in a view directly, you can make it static but it's up to you. You can do it in that way as well.

    Since I don't know your case in detail, my suggestions are superficial.

  • User Avatar
    0
    bilalhaidar created

    Do you have a similar case in the framework where you needed to call the Database from the Razor View? I can at least follow the code and see the way you thought about it.

    Thanks, Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Actually we don't access to DB directly from views. Actually I'm a bit confused about your case :).

    Maybe I didn't understand it very well. Have you written any code about this ? If you can share it maybe we can suggest you something.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    For example, @L() isn't this somewhere referring to a class in the solution that connects to the Db? I was planning to do something similar.

    A separate IVisibilityChecker and VisibilityChecker classes, inject them into the base View Page, then be able to access the IfShown or IfHidden, etc.

    Other way around, is in the header of the .cshtml file, I might also create an instance of some class that connects to Db.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Yes, you can follow a similar approach to @L() method definition. You can also access to DB from views but we don't suggest it directly.

    Why don't you filter the data in the app service and pass the filtered data to your viewmodel and render only the visible data in your view ?

  • User Avatar
    0
    bilalhaidar created

    On the client side (angular) you mean?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    What I meant is: Actually you need to do it on the client and server side. For different user's same app service will return different data and angular view on the client side will handle showing it to user properly.

    But AspNet Zero does not offer or force any approach in this case. This is a design question actually and you can do it as you would do in any other Web project.

  • User Avatar
    0
    bilalhaidar created

    Actually you need to do it on the client and server side.

    You mean that on the server-side I write the code that will return the meta data for a form (which fields to show and hide). On the client side I use Angular to show/hide.

    Originally I thought in addition to writing code on server side to know what field to show and what to hide, I thought I can do the filtering on razor view server side rather than angular.

    By the way, what's your first name?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes actually both methods (my suggestion and your idea) are valid and good ways of doing it. Difference is developer's choice :).

    By the way, my first name is Ismail.

  • User Avatar
    0
    bilalhaidar created

    Thanks Ismail,

    Once I reach the stage of deciding which way to go, I will rethink it more.

    So my options are:

    1. Extend base view page in Razor and include a method to be used in Razor to decide if a field is to be shown or hidden
    2. Other option, bring in those rules to angular and then based on them, I show/hide fields on client side.

    Maybe for the second option and to avoid showing fields all at once before the rules are loaded, is to resolve a property by calling server side to bring in the rules.

    Regards Bilal