0

I'm trying to learn unobtrusive-ajax, so I don't have to reload the whole page but only the content of the table, with a PartialView.

PartialView isn't loaded probably into table. Is it a routing problem that I have?

URL with table: http://localhost:50459/

URL after trying to load new rows to table http://localhost:50459/Product/GetProductData

RouteConfig:

using System.Web.Mvc;
using System.Web.Routing;

namespace MVCUnobtrusiveAjax
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Product", action = "GetProducts", id = UrlParameter.Optional }
            );
        }
    }
}

cshtml:

@using MVCUnobtrusiveAjax.Models;
@model string

@{
    ViewBag.Title = "Get Products";
    AjaxOptions ajaxOptions = new AjaxOptions
    {
        UpdateTargetId = "productsTable"
    };
}

<h2>Get Products</h2>

<div id="loadingProducts" style="background-color:cadetblue; display:none">
    <p>Loading Products...</p>
</div>

<table style="background-color:lightcoral">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Description</th>
            <th>Category</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody id="productsTable">
       @Html.Action("GetProductData", new { selectedCategory = Model })
    </tbody>
</table>

@using (Ajax.BeginForm("GetProductData", ajaxOptions))
{
    <div>
        @Html.DropDownList("selectedCategory", new SelectList(
            new[] { "All" }.Concat(Enum.GetNames(typeof(Category)))
        ))

        <button type="submit">Submit</button>
    </div>
}

PartialView

@using MVCUnobtrusiveAjax.Models
@model IEnumerable<Product>

@foreach (Product p in Model)
{
    <tr>
        <td>@p.ProductId</td>
        <td>@p.Name</td>
        <td>@p.Description</td>
        <td>@p.Category</td>
        <td>@p.Price</td>
    </tr>
}

ProductController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MVCUnobtrusiveAjax.Models;

namespace MVCUnobtrusiveAjax.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public PartialViewResult GetProductData(string selectedCategory = "All")
        {
            IEnumerable<Product> data = products;
            if (selectedCategory != "All")
            {
                Category selected = (Category)Enum.Parse(typeof(Category), selectedCategory);
                data = products.Where(p => p.Category == selected);
            }

            return PartialView(data);
        }

        public ActionResult GetProducts(string selectedCategory = "All")
        {
            return View((object) selectedCategory);
        }

        private Product[] products = {
                                         new Product {
                                             ProductId = 1, Name = "Audi A3",
                                             Description = "New Audi A3", Category = Category.Car,
                                             Price = 25000
                                         },
                                         new Product {
                                             ProductId = 2, Name = "VW Golf",
                                             Description = "New VW Golf", Category = Category.Car,
                                             Price = 22000
                                         },
                                         new Product {
                                             ProductId = 3, Name = "Boing 747",
                                             Description = "The new Boing airplane", Category = Category.Airplane,
                                             Price = 2000000
                                         },
                                         new Product {
                                             ProductId = 4, Name = "Boing 747",
                                             Description = "The new Boing airplane", Category = Category.Airplane,
                                             Price = 2000000
                                         },
                                         new Product {
                                             ProductId = 5, Name = "Yamaha 250",
                                             Description = "Yamaha's new motorcycle", Category = Category.Motorcycle,
                                             Price = 5000
                                         },
                                         new Product {
                                             ProductId = 6, Name = "honda 750",
                                             Description = "Honda's new motorcycle", Category = Category.Motorcycle,
                                             Price = 7000
                                         }

                                     };
    }
}
6
  • Try this: Ajax.BeginForm("GetProductData", "Product", ajaxOptions) Commented Jun 5, 2014 at 16:06
  • It still changes to http://localhost:50459/Product/GetProductData Commented Jun 5, 2014 at 16:12
  • I should add clearify that http://localhost:50459/ shows the list and button. and http://localhost:50459/Product/GetProductData shows the single rows from the partial view. Commented Jun 5, 2014 at 16:16
  • 1
    You make the you referenced the proper js files? Commented Jun 5, 2014 at 16:23
  • oh you are a god, thanks. It works now, you were right, I forgot to reference the .js script. <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> Commented Jun 5, 2014 at 16:33

1 Answer 1

1

Make you have have referenced the proper js file. For unobtrusive, it would be:

jquery.unobtrusive-ajax.min.js

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.