Independence in the Context of a view using Partial or RenderPartial asp.net mvc

-1

Hello, people could help me

See if I explain:

I have an Index view that has a table and includes several times the same partial view (prev call) in the spaces of the table, each (prev.cshtml) with different size, within the partial view (prev) add a javascript method to detect the size of the main 'div' of each (prev) using the id of the main div of prev.    The problem is that it seems that every partial view (prev) has no independence, because when executing, the js function always gives me the size of the first (prev), as if the function were executed once with each (prev) but in the scope of the parent view, it is like having each main div of the prev the same id the function always works on the first one, which would not happen if the partial views had something like an independent context.

    
asked by Matias Middle 06.01.2018 в 15:27
source

1 answer

1

You must bear in mind that "partial views" is a concept that only exists in the server and that serves precisely to encapsulate code of the views that is repeated several times in different places.

However, once the view is processed (with the different partial views included), the output generated by the ASP.Net MVC view engine will be the HTML code of a single page to be displayed in a browser window . As of that moment, there is no division between the code generated by the main view, the layout or the partial views.

That is why, when the browser shows the received HTML code in the window, it shows the page generated from this HTML code, building with its elements a single DOM tree. This is something that you should take into account when generating the id s of the elements (since these should be unique).

You should also avoid including javascript code in partial views and extract it to external files. This way you will avoid, for example, defining the same function several times when you include the same partial view several times.

The browser, therefore, is agnostic regarding how the received HTML code has been generated and is not aware of the existence of partial views.

    
answered by 06.01.2018 / 15:55
source