MVC 4 Razor Object doesn't support property or method 'dataTable'

后端 未结 3 1486
梦毁少年i
梦毁少年i 2021-01-24 18:46

I\'m a noob to MVC, jquery and DataTables. I get a Microsoft Visual Studio warning dialog when I hit F5 to build and bring up the site locally for development/debug:

3条回答
  •  囚心锁ツ
    2021-01-24 19:30

    Since you mention that you are a newbie (i dont like the word noob) in MVC and such, I assume that you declared jQuery in more than one Views (like you did above). I am providing an example layout for you code, if you wish to follow it

    jQuery and any other libraries, should only be included in your html only once, ideally at the bottom of the page, just before the body closing tag

    If at later point in your code, you are including jQuery again, then it will overwrite the previous one, which has the dataTable plugin attached to it

    Best to check in an actual browser (chrome,firefox,IE) instead of Visual Studio. In Chrome, press F12 to bring up the Developer Tools, then press Esc to bring up the console. See any errors there?


    An example layout for your code could look like this. This is the master view file (If i am not mistaken, by default its in Views > Shared > _Layout.cshtml)

    
    
        
            ...
    
            
            
            
            @if (IsSectionDefined("Styles"))
            { 
                @RenderSection("Styles")
            }
    
            ...
        
        
            ...
            
            
            
            @if (IsSectionDefined("Scripts"))
            { 
                @RenderSection("Scripts")
            }
        
    
    

    And you can add any specific styles/scripts you want to each view, like so

    ... 
    
    ...
    
    @section Styles {
           
    }
    
    @section Scripts {
        
    }
    

    Hope it helps

提交回复
热议问题