ASP .NET MVC 3 设计问题(通过 Ajax 加载数据)
我有一个类别类别,代表产品类别... 所以我创建了一个视图,显示一个树视图,左侧有我的类别...... 当用户选择 TreeView 节点时,我想在同一页面/视图中显示所有类别数据(右侧)
到目前为止我所做的是通过 Ajax 加载 JQuery TreeView...并在每个 TreeView 上添加了一个单击事件节点... 单击通过 Ajax 调用所选类别,如下所示 (CategoryController):
[HttpGet]
public ActionResult GetCategory(string idCategory)
{
if (ModelState.IsValid)
{
var _category = _categoryRepository.Get(Convert.ToInt16(idCategory));
if (Request.IsAjaxRequest())
return Json(_category, JsonRequestBehavior.AllowGet);
return RedirectToAction("Index");
}
return View();
}
因此,我在客户端上获取了类别 JSON,并且必须手动填写所有字段...
这是正确/最佳的方法吗? 我错过了什么吗?
到目前为止谢谢
保罗
I have a class Category, that represents products category...
So I created a View that shows a Treeview with my Categories on left ...
When user select a TreeView Node, I´d like to show all the Category data in the same Page/View (On right)
What I did so far is load the JQuery TreeView via Ajax... And added a click event on each TreeView Node...
The click calls, via Ajax, the selected Category, like that (CategoryController):
[HttpGet]
public ActionResult GetCategory(string idCategory)
{
if (ModelState.IsValid)
{
var _category = _categoryRepository.Get(Convert.ToInt16(idCategory));
if (Request.IsAjaxRequest())
return Json(_category, JsonRequestBehavior.AllowGet);
return RedirectToAction("Index");
}
return View();
}
So, I got the Category JSON on client, and have to manually fill all the fields ...
Is that the right/best way to do that?
Am I missing something?
Thanks so far
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 JSTree,则可以使用 JSON_DATA 插件从服务器返回 JSON 数据,并让 JSTree 为您构建节点。我不知道您的
_category
对象是什么样的,但文档提供了 JSTree 期望的结构。如果您无法以这种格式返回数据,那么您必须像现在一样手动渲染树节点。
我希望这有帮助!
If you're using JSTree, you can use the JSON_DATA plugin to return JSON data from your server and have JSTree build your nodes for you. I don't know what your
_category
object looks like, but the documentation provides the structure that JSTree expects.If you can't return data in this format, then you'll have to render the tree nodes manually as you're doing it now.
I hope this helps!
我解决了使用 KnockoutJs 和地图插件的问题...效果很好!
无论如何,谢谢...
保罗
I resolve that using KnockoutJs with mapping plugin... It work great!
Thanks anyway...
Paul