locked
ASP.NET MVC Ajax example by Scott Allen isn't working RRS feed

  • Question

  • User-315219446 posted

    i am watching videos of Scott Allen at pluralsight site, exactly I am talking about this module : https://app.pluralsight.com/player?author=scott-allen&name=mvc4-building-m6-ajax&mode=live&clip=0&course=mvc4-building

    So i created exactly the same example as shown at video, but it is not working, I have no idea why and  I spent an hours looking for this, here is my code:

    At BoundleConfig.cs file:

     bundles.Add(new ScriptBundle("~/bundles/otf").Include(
                "~/Scripts/jquery-{version}.js",
                "~/Scripts/jquery-ui-{version}.js",
                "~/Scripts/jquery.unobtrusive-ajax.min.js",
                "~/Scripts/jquery.validate*"
                ));
    At Web.config file in appSettings section I added:
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    Controller action :
    public ActionResult Review(string searchString)
        {
    
            var model = _repository.StartDs;
    
    
            if (!String.IsNullOrEmpty(searchString))
            {
                model = model.Where(x => x.FirstName.Contains(searchString));
            }
    
    
    
    
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Review", model);
            }
    
            return View(model);
    
        }
    Review.cshtml file :

    @model IEnumerable<nauka.Models.StartData>
    @using(Ajax.BeginForm(
                    new AjaxOptions
                    {
                        HttpMethod = "get",
                        InsertionMode = InsertionMode.Replace,
                        UpdateTargetId = "girlsList"
                    }))
    {
        <input type="search"  name="searchString"/>
        <input type="submit" value="Znajdź" />
    
    }
        @Html.Partial("_Review", Model)
    _Review.cshtml file:
    @model IEnumerable<nauka.Models.StartData>
    
    <div id="girlsList">
        <table style="width:100%">
    
            <tr>
    
                <th>ID</th>
                <th>Imię</th>
                <th>Nazwisko</th>
                <th>Wzrost</th>
                <th>Rozmiar</th>
                <th>Akcje</th>
    
    
    
            </tr>
    
    
            @foreach (var item in Model)
            {
    
                <tr>
                    <td>@item.Id</td>
                    <td>@item.FirstName</td>
                    <td>@item.LastName</td>
                    <td>@item.Height</td>
                    <td>@item.TitsSize</td>
                    <td>@Html.ActionLink("Usuń", "Delete", new { id = item.Id })</td>
                </tr>
    
    
    
            }
    
        </table>
    
    </div>
    Above my _Layout.cshtml file which is automatically added to every view page:
       <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - My ASP.NET Application</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    
    
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        <li>@Html.ActionLink("Dodaj", "Add", "Home")</li>
                        <li>@Html.ActionLink("Przegladaj", "Review", "Home")</li>
                        <li>@Html.ActionLink("Filtruj", "FilterTest", "Home")</li>
    
    
                    </ul>
                    @Html.Partial("_LoginPartial")
                </div>
            </div>
        </div>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/otf")
        @RenderSection("scripts", required: false)
    </body>
    </html>
    
    What I know is that this part of Review action : 
    if (Request.IsAjaxRequest())
            {
                return PartialView("_Review", model);
            }
    Never execute, because ona time I replaced it with return View("Index"), and the site didn't redirected me to the index site when i clicked search
    So where is a solution, I have no other idea, I think that I did everything.
    help!


    Wednesday, June 29, 2016 6:24 PM

Answers

  • User-315219446 posted

    Ough... solved!! 

    I didn't add a 

    jquery.unobtrusive-ajax.min.js  library to my solution through NuGet package installer.
    But when I added this before to BoudleConfig ( path
     "~/Scripts/jquery.unobtrusive-ajax.min.js",), why it didn't shout that this library isn't present at my project in Visual ??!
    It made many complications..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2016 6:36 PM

All replies

  • User-315219446 posted

    Ough... solved!! 

    I didn't add a 

    jquery.unobtrusive-ajax.min.js  library to my solution through NuGet package installer.
    But when I added this before to BoudleConfig ( path
     "~/Scripts/jquery.unobtrusive-ajax.min.js",), why it didn't shout that this library isn't present at my project in Visual ??!
    It made many complications..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2016 6:36 PM
  • User61956409 posted

    Hi mike_pl,

    Firstly, I’m glad to hear that you find and fix the issue by yourself.

    Besides, as you said, Visual Studio does not help us check if the JS files are existing under a folder when we combine or bundle multiple files into a single file. We could check if specific files are existing before we add them in bundle.

    Best Regards,

    Fei Han

    Thursday, June 30, 2016 2:52 AM