我想知道完成此任务的最佳方法是什么: 在我的索引方法中,我要么搜索,要么过滤,或者只是采取 来 self 的数据库的数据。然后我发送 n 个项目来查看。如果有超过n个项目 我需要完成一些分页。据我了解,我可以再次查询我的数据库 过滤或搜索并获取下一个 n 元素,或者我可以以某种方式保留所有元素 我的数据(过滤后或搜索查询后收到的数据) 从中取出 n 个下一个元素。据我了解, session 不是最好的方法 为了完成它,所以代替这个类的 IEnumerable
public class CatalogModel
{
public string Price;
public string deviceName;
public string imgUrl;
}
我决定使用这个类
public class CatalogView
{
public IEnumerable<CatalogModel> Catalog;
public IEnumerable<Device> Devices;
public CatalogView(IEnumerable<CatalogModel> catalog = null, IEnumerable<Device> devices = null)
{
Catalog = catalog;
Devices = devices;
}
}
我想将我的所有数据保存在
public IEnumerable<Device> Devices
这个字段每次发送到 Controller , 但我不知道如何从 javascript 访问它,以便我可以将其发回 用 Ajax 。
是否可以这样做,并且它会比 每次查询我的数据库。
谢谢!
最佳答案
您应该首先在接受 CatalogView 对象的 Controller 中执行操作。
要创建与 C# 对象类似的对象,可以使用此行
var obj = @Html.Raw(Json.Encode(new CatalogView())
将代码修改为
public class CatalogView { public IEnumerable<CatalogModel> Catalog; public IEnumerable<Device> Devices; public CatalogView(IEnumerable<CatalogModel> catalog = null, IEnumerable<Device> devices = null) { Catalog = catalog; Devices = devices; } public CatalogView () { Catalog = new List<CatalogModel>(); Devices = new List<Device>(); } }
并使所有公共(public)字段成为公共(public)属性,以便与 JSON 相互转换 4.对于javascript,您可以使用以下代码片段
$.ajax({ type: "post", url: "@Url.Action("YourActionName","ControllerName")", data: JSON.stringify(obj), contentType: "application/json", success: function (details) { //execute on success } });
您应该使用http://www.json.org/js.html为了将你的对象转换为 json
关于c# - 使用 ajax 将模型发送到 Controller ,asp.net mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403626/