What is the point of Partial Views in Asp.net MVC

后端 未结 8 1910
北恋
北恋 2020-12-30 00:49

Ive noticed that there seems to be no real difference between a view and a partial view. For instance, one can create a view but can render it as a

相关标签:
8条回答
  • 2020-12-30 01:34

    To answer your question specifically, when adding a new view in Visual Studio, you will get some very basic markup generated for you as a starting point, based off of your selections in the dialog.

    Here is the generated markup in Visual Studio 2010 (VB.NET) for the different combinations of the "Partial" checkbox and the "Layout" checkbox:

    # "Create as a partial view" unchecked
    # "Use a layout or master page:" unchecked
    
    @Code
        Layout = Nothing
    End Code
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>MyView</title>
    </head>
    <body>
        <div>
    
        </div>
    </body>
    </html>
    
    # "Create as a partial view" unchecked
    # "Use a layout or master page:" checked
    
    @Code
        ViewData("Title") = "MyView"
        Layout = "~/ThePath/ToThe/Layout.vbhtml"
    End Code
    
    <h2>MyView</h2>
    
    # "Create as a partial view" checked
    # "Use a layout or master page:" greyed out
    
    # returns an empty file
    

    As you can see there is nothing fancy going on in the background or special properties being set in a secret file somewhere. The options are simply used to get some default markup on the page. Whether or not this is practical is purely subjective!

    0 讨论(0)
  • 2020-12-30 01:36

    Quite late but might be useful for someone with the same question. Partial views are helpful in a scenario where you want to load a view based on some user selection.

    For instance, let's assume there is a dropdown in parent view displaying three operations that the user can perform. Based on the user selection, a partial view can be loaded into the parent view instead of keeping hidden DIVs in the parent view itself, thus making the parent view light. This will be very useful when we have multiple such user selections based DIVs

    0 讨论(0)
提交回复
热议问题