我目前正在編碼ViewComponent
(ViewComponent1)視圖。在此視圖中,我列出了幾項:在另一個ViewComponent中調用ViewComponent
正如您所看到的,通道11,12,13和14是可點擊的。每個頻道都有一些附加信息(OBIS,avtalsid等)。我想要做的是調用ViewComponent1中的ViewComponent2,並根據點擊的項目傳遞一些數據。
我試圖做的是創建另一個View
名爲「測試」和View
中調用ViewComponent2其參數一起,就像這樣:
<div class="row">
<div class="col-md-2 canalstyle">
<a asp-controller="Customer" asp-action="Test" asp-route-pod="@item.STATION"
asp-route-idnr="@item.IDNR" asp-route-kanal="@item.KANAL" asp-route-start="@Model.start"
asp-route-end="@Model.end"> @Html.DisplayFor(modelItem => item.KANAL)</a>
</div>
</div>
這工作,但這種方法重定向我遠離我當前View
(ViewComponent 1)。我不想那樣。我想要當前視圖從ViewComponent2加載附加信息。
我的功能在運行AJAX:
function myFunction() {
var data = JSON.stringify({
'idnr': id,
'start': this.start,
'end': this.end
});
$.ajax({
url: '@Url.Action("Test2","Customer")',
type: 'GET',
data: { idnr: id, start: this.start, end: this.end },
contentType: 'application/json',
success: handleData(data)
})
};
function handleData(data) {
alert(data);
var url = $(this).attr("href");
var $target = $(this).closest("div").find(".details");
$.get(url, function (res) {
$target.html(res);
});
//do some stuff
}
而且我的Test2操作:
public async Task<IActionResult> Test2(string idnr, string start, string end)
{
ServiceClient r2s = new R2S.ServiceClient();
R2S.Konstant[] kData = r2s.GetKonstantListAsync(new string[] { "IDNR" }, new string[] { idnr}).Result; // mätarnummer in... --> alla konstanter kopplade till denna.
return ViewComponent("MeterReader2", new { k = kData[0], start = start, end = end });
}
我努力的目標相同的DOM ..任何想法?
我還有一個後續的問題...是否有可能,從上面的方法,傳遞一些基於javascript值的路由值?我有兩個帶有日期的文本字段,我想將它們傳遞給相同的動作... – jazy
是的。你可以用你想要的任何路由鍵值更新你的js代碼中的url變量。 – Shyju
如何實現該url變量,如果讓我說我有JavaScript變量this.start和this.end?我用額外的2個路由鍵值擴展了你的代碼:asp-route-start =「」asp-route-end =「」 – jazy