Asp.net mvc 加载partialview会使用不引人注目的ajax更改URL路由
我正在尝试学习Asp.net mvc 加载partialview会使用不引人注目的ajax更改URL路由,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我正在尝试学习低调的ajax,因此我不必重新加载整个页面,只需使用PartialView重新加载表的内容 PartialView可能未加载到表中。这是我遇到的路由问题吗 带有表格的URL: http://localhost:50459/ 尝试将新行加载到表后的URL http://localhost:50459/Product/GetProductData RouteConfig: using System.Web.Mvc; using System.Web.Routing; namespac
低调的ajax
,因此我不必重新加载整个页面,只需使用PartialView
重新加载表的内容
PartialView
可能未加载到表中。这是我遇到的路由问题吗
带有表格的URL:
http://localhost:50459/
尝试将新行加载到表后的URL
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>
}
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
}
};
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Mvc;
使用mvcunobstrusiveajax.Models;
命名空间MVCUNobstrusiveAjax.Controllers
{
公共类ProductController:控制器
{
//
//获取:/Product/
public PartialViewResult GetProductData(字符串selectedCategory=“All”)
{
IEnumerable数据=产品;
如果(selectedCategory!=“全部”)
{
所选类别=(类别)Enum.Parse(typeof(类别),selectedCategory);
数据=产品。其中(p=>p.类别==所选);
}
返回部分视图(数据);
}
public ActionResult GetProducts(string selectedCategory=“All”)
{
返回视图((对象)selectedCategory);
}
私人产品[]产品={
新产品{
ProductId=1,Name=“奥迪A3”,
Description=“新款奥迪A3”,类别=类别汽车,
价格=25000
},
新产品{
ProductId=2,Name=“大众高尔夫”,
Description=“新款大众高尔夫”,Category=Category.Car,
价格=22000
},
新产品{
ProductId=3,Name=“Boing 747”,
Description=“新波音飞机”,类别=类别飞机,
价格=2000000
},
新产品{
ProductId=4,Name=“Boing 747”,
Description=“新波音飞机”,类别=类别飞机,
价格=2000000
},
新产品{
ProductId=5,Name=“雅马哈250”,
Description=“雅马哈的新摩托车”,类别=类别。摩托车,
价格=5000
},
新产品{
ProductId=6,Name=“本田750”,
Description=“本田新摩托车”,Category=Category.摩托车,
价格=7000
}
};
}
}
确保您已经引用了正确的js文件。对于不引人注目的,应该是:
jquery.unobtrusiveajax.min.js
试试这个:ajax.BeginForm(“GetProductData”、“Product”、ajaxOptions)
它仍然会变为http://localhost:50459/Product/GetProductData
我应该添加clearify,使http://localhost:50459/
显示列表和按钮。和http://localhost:50459/Product/GetProductData
显示了部分视图中的单行。如果引用了正确的js文件,您会做出正确的选择吗?哦,您是上帝,谢谢。它现在可以工作了,你是对的,我忘了引用.js脚本。
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
}
};
}
}