如何将 ParentNode 值从 JQuery TreeView 传递到 ASP.NET MVC 页面上的控制器方法?
我有一个 ASP.NET MVC 页面,该页面具有使用 JQuery Treeview 控件动态构建的左侧菜单导航。
此树视图将 ProductNames 列表作为父节点(例如:10 个产品)。这些产品中的每一个都有一个 ChildNode 作为 DocTypeName(例如:3 个 DocTypeName)。
这里,当用户单击 ParentNode 时,它会展开并显示 DocTypeNames。当用户点击DocTypeName时,它通过Ajaxy方式调用控制器ActionResult DocumentDetails来加载partialView。
从下面的代码中,我可以读取单击的 DocTypeName。但我无法读取产品名称。它说“未定义”。
有人知道如何将父产品名称传递给控制器吗?
NavigationProducts.ascx 页面:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MedInfoDS.Controllers.ProductViewModel>" %>
<script type="text/javascript">
$(document).ready(function () {
$(".docId").click(function () {
alert("DocTypeName: " + this.id);
alert("ProductName: " + this.ProductName); //Error throwing here "Undefined"
$("#docDetails").load('<%= Url.Action("DocumentDetails") %>', { ProductName: "darbepoetin alfa", DocTypeName: this.id }, function (responseText, status) {
});
return false;
});
});
<div id="treecontrol">
<a title="Collapse the entire tree below" href="#">Collapse All</a> | <a title="Expand the entire tree below"
href="#">Expand All</a> | <a title="Toggle the tree below, opening closed branches, closing open branches"
href="#">Toggle All</a>
<div id="divByProduct">
<ul id="red" class="treeview-red">
<% foreach (var item in Model.Products)
{ %>
<li><span>
<%=item.Name%></span>
<ul>
<%foreach (var item1 in Model.DocTypes) { %>
<li><span>
<%= Html.ActionLink(item1.DocTypeName, "Products", new { ProductName = item.Name, DocTypeName = item1.DocTypeName })%>
<br />
<a class="docId" href="#" id="<%=item1.DocTypeName%>"><%= item1.DocTypeName%></a>
<%= Html.Hidden("ProductName", item.Name)%>
</span></li>
<% } %>
</ul>
</li>
<% } %>
</ul>
控制器方法:
// Response to AJAXy call to populate details for given ProductName and DocType
[HttpPost]
public virtual ActionResult DocumentDetails(string ProductName, string DocTypeName)
{
var entities = new MIDSContainer();
if (ProductName == null) return View();
int ProductId = (entities.Products.FirstOrDefault(p => p.Name == ProductName)).ProductId;
int DocTypeId = (entities.DocTypes.FirstOrDefault(d => d.DocTypeName == DocTypeName)).DocTypeId;
var documents = (from d in entities.Documents.Where(p => p.ProductId == ProductId && p.DocTypeId == DocTypeId && p.AvailableForUse == true && p.Status == "Approved") orderby (d.Description) select d).ToList();
return View(documents);
}
I have an ASP.NET MVC page that has left Menu Navigation that is built dynamically using JQuery Treeview Control.
This Treeview has the list of ProductNames as the Parent Node(For Example: 10 Products). Each of these Products have a ChildNode as DocTypeName(For Example: 3 DocTypeNames).
Here When the user clicks on ParentNode, it expands and shows DocTypeNames. When the User Clicks on DocTypeName, it loads the partialView by calling the controller ActionResult DocumentDetails through Ajaxy way.
From the below code I am able to read the DocTypeName that is clicked. But I am not able to read the ProductName. It says "Undefined".
Anyone has any idea how to pass the Parent ProductName to the controller?
NavigationProducts.ascx Page:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MedInfoDS.Controllers.ProductViewModel>" %>
<script type="text/javascript">
$(document).ready(function () {
$(".docId").click(function () {
alert("DocTypeName: " + this.id);
alert("ProductName: " + this.ProductName); //Error throwing here "Undefined"
$("#docDetails").load('<%= Url.Action("DocumentDetails") %>', { ProductName: "darbepoetin alfa", DocTypeName: this.id }, function (responseText, status) {
});
return false;
});
});
<div id="treecontrol">
<a title="Collapse the entire tree below" href="#">Collapse All</a> | <a title="Expand the entire tree below"
href="#">Expand All</a> | <a title="Toggle the tree below, opening closed branches, closing open branches"
href="#">Toggle All</a>
<div id="divByProduct">
<ul id="red" class="treeview-red">
<% foreach (var item in Model.Products)
{ %>
<li><span>
<%=item.Name%></span>
<ul>
<%foreach (var item1 in Model.DocTypes) { %>
<li><span>
<%= Html.ActionLink(item1.DocTypeName, "Products", new { ProductName = item.Name, DocTypeName = item1.DocTypeName })%>
<br />
<a class="docId" href="#" id="<%=item1.DocTypeName%>"><%= item1.DocTypeName%></a>
<%= Html.Hidden("ProductName", item.Name)%>
</span></li>
<% } %>
</ul>
</li>
<% } %>
</ul>
Controller Method:
// Response to AJAXy call to populate details for given ProductName and DocType
[HttpPost]
public virtual ActionResult DocumentDetails(string ProductName, string DocTypeName)
{
var entities = new MIDSContainer();
if (ProductName == null) return View();
int ProductId = (entities.Products.FirstOrDefault(p => p.Name == ProductName)).ProductId;
int DocTypeId = (entities.DocTypes.FirstOrDefault(d => d.DocTypeName == DocTypeName)).DocTypeId;
var documents = (from d in entities.Documents.Where(p => p.ProductId == ProductId && p.DocTypeId == DocTypeId && p.AvailableForUse == true && p.Status == "Approved") orderby (d.Description) select d).ToList();
return View(documents);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
Html.Hidden("ProductName", item.Name)
呈现一个 ID 为 ProductName 的隐藏字段,那么您可能在一页上有多个具有相同 ID 的元素,因此对其的查找可能会返回 undefined。否则,查找 jQuery 查询只是
$("#ProductName").val()
if
Html.Hidden("ProductName", item.Name)
renders a hidden field with id ProductName then you might have multiple elements with the same ID on one page and therefore the lookup for it might return undefined.Otherwise the lookup jQuery query is just
$("#ProductName").val()
尝试将其添加到发送到服务器的数据中:
ProductName: $(this).siblings(":hidden").val()
但是您绝对应该避免生成具有相同内容的多个元素ID!
Try to add this to the data that you send to the server:
ProductName: $(this).siblings(":hidden").val()
But you shoud definitely avoid generating multiple elements with the same id!