0
我的類和視圖模型,通過JavaScript訪問嵌套模型在asp.net MVC
public class MyClass {
[Key] public int Id {get;set;}
public name {get;set;} ....etc.
}
和
public class MajorViewModel {
[key] public string id{get;set;}
public List<MyClass> myClass {get;set;}
...etc;
}
在控制器
public ActionResult viewDetail() {
MajorVeiwModel mv = new MajorViewModel()
...
..
return View(mv);
}
兒童局部視圖
@model namespace.MyClass
<div> @html.DisplayNameFor(model=>model.id)
<div> @html.DisplayFor(model=>model.id)
....and tens of other properties..
看來,我有
@model namespace.MajorViewModel
<div> @html.DisplayNameFor(model=>model.id)
@html.DisplayFor(model=>model.id) </div>
@for(var I=0; I< Model.myClass.count(); i++)
{
<div>@Html.DisplayFor(model => model.myClass[i].id)
<a href="#" id="displayId" data-bind: @Model.myClass[i]) <==not sure how to pass data
}
<script>
$("#displayId").click(function(){
alert(this.name); <== how to access MyClass[i].name for respective row for which it was clicked.
var resultDiv = $('<div id="childView"></div>');
var htmlContext = $(this).parent();
//somehow get the model selected and create html for child
var result = ??? <== fill it with child partial view html
//TODO ...FILL THE result
resultDiv.html(result);
alert(resultDiv);
});
</script>
在上面的代碼,我想要綁定的每一行/鏈接數據,但不知道如何綁定,以便在鏈接點擊,在JavaScript中我能訪問所有第i個實例的數據(MyClass [i] .name等數十個屬性)由用戶點擊並顯示。 我是否需要序列化數據,但如何鏈接每行?沒有桌子。使用@ Html.HiddenFor。
將屬性值添加爲'data-'屬性 - 例如'
@Stephen,我想傳遞@ Model.myClass [i]類型的myClass的整個模型,以便強制類型化爲myClass創建的視圖。對話窗口請注意,我評論說有幾十個屬性,如MyClass [i] .name等)「 – AKS
您需要添加每個單獨的屬性 - 您不能將'model'添加到'data-'屬性。另一種方法是隻使用相對選擇器來獲取與'DisplayFor()'方法相關的文本 –