JQuery Treeview Controlを使用して動的に構築されたMenu Navigationを終了したASP.NET MVCページがあります。ASP.NET MVCページのJQueryツリービューからParentNode値をControllerメソッドに渡すにはどうすればよいですか?
このツリービューは、(例:10製品)親ノードとしてProductNamesのリストを有しています。これらの製品のそれぞれはChildNodeとしてDocTypeName(例:3 DocTypeNames)です。
ここで、ParentNodeをクリックすると、DocTypeNamesが展開されて表示されます。ユーザーがDocTypeNameをクリックすると、コントローラActionResult DocumentDetailsをAjaxy経由で呼び出して、partialViewをロードします。
以下のコードから、私はクリックされたDocTypeNameを読むことができます。しかし、私はProductNameを読むことができません。それは "未定義"と言います。
Parent ProductNameをコントローラーに渡す方法については、誰でも知っていますか?
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>
コントローラ方法:Html.Hidden("ProductName", item.Name)
がID商品名で隠しフィールドをレンダリング
// 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);
}
Html.Hidden(...)の結果はどうなりますか?私はJSを理解しますが、ASP.NET MVCは理解しません。 – tillda
ProductNameの値が次のようになっています。 – Rita