0

How do I integrate jquery with mvc framework, i have created a sample application and i have the directories named app_data, Content, Controllers, Model, Views and many others . The controller folder contains All the controllers written in c#.So where should i write the jquery script, should i write it in layout.cshtml or in index.cshtml ? How do i integrate jquery with asp.net mvc framework.

6
  • Is there a master page file in your Project? Commented Feb 11, 2013 at 5:02
  • Yes the master page is locate in view /-Layout.cshtml Commented Feb 11, 2013 at 5:05
  • Basically Jquery is for client side programming ... So you can include the your jquery library in your master page file or in that particular page itself and write all your jquery script in the view file which you want your jquery code to work.. For eg. if you want to pop up a new div in index.aspx view page, write your jquery code in that aspx file. Commented Feb 11, 2013 at 5:11
  • 1
    If you include _Layout.cshtml to all your views, then simply write @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") in your Layout page. Include a Layout="path of layout" in a section in your view and write your jQuery in your views Commented Feb 11, 2013 at 5:14
  • @karthik Sir by Layout Page do you mean (_Layout.cshtml) ? Commented Feb 11, 2013 at 5:17

2 Answers 2

1

As you know in MVC, there is a Layout.cshtml file.

This is what my _Layout.cshtml contains. The below code for _Layout.cshtml is as per my requirement.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Your title</title>
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/jqueryui")
</head>
<body>
            <div id="body">
                <section class="content-wrapper main-content clear-fix">
                    @RenderBody()
                </section>
            </div>
//blah blah
</body>
</html>

If you were to include your Master Page(_Layout.cshtml) in all your views you can just include your scripts in _Layout page and refer to that _Layout.cshtml page in your view.

This is how you should refer to your _Layout.cshtml file in your view.

On top of your view, you should write a code block like this inorder to add your MasterPage reference.

@{
      ViewBag.Title= "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
}

Now go a head and write your jQuery code that you needed in your view:

<script type="text/javascript">
    $(function(){
        //your jQuery goes here
    });
</script>

Finally your view should look like this :

@{
    ViewBag.Title= "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<head>
    <style>
    </style>
    <script type="text/javascript">
    $(function(){
        //your jQuery goes here
    });
</script>
</head>
<body>
    @: This is my Index View
</body>
</html>

Hope it helps.

If anything went wrong please tell me.

Sign up to request clarification or add additional context in comments.

3 Comments

@Scripts.Render("~/bundles/jqueryui") What does "~" character signify?
It is called as tilde, when you use relative paths, if you use ~ it gets you from the application root.
this is a pretty exhaustive tutorial. @Falcon, Karthik can only take you so far. At this point you should be able to implement with your own research.
0

please follow below url for MVC with Jquery with proper architecture

http://www.codeproject.com/Articles/344292/ASP-NET-MVC3-Razor-With-jQuery-For-Beginners

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.