[英]Get selected value of Kendo grid on another View
所以昨天我開始學習一個項目的JavaScript和Web開發。 我們正在使用MVC模式,但是在弄清楚javascript類如何與視圖一起工作時遇到了問題。 任何幫助,將不勝感激。 就像我說的那樣,我的知識非常有限。 但是,我確實了解C#和WPF(MVVM),所以也許其中的一些知識會對我有所幫助。
我們使用劍道控件。 下面是我們kendo網格的一些JavaScript。
grid.js:
function onChange(e) {
//get currently selected dataItem
var grid = e.sender;
var selectedRow = grid.select();
var dataItem = grid.dataItem(selectedRow);
var y = $.ajax({
url: "/api/ServiceOrderData/" + dataItem.id,
type: 'GET',
dataType: 'json'
});
}
$("#serviceOrderList").kendoGrid({
groupable: true,
scrollable: true,
change: onChange,
sortable: true,
selectable: "row",
resizable: true,
pageable: true,
height: 420,
columns: [
{ field: 'PriorityCodeName', title: ' ', width: 50 },
{ field: 'ServiceOrderNumber', title: 'SO#' },
{ field: 'ServiceOrderTypeName', title: 'Type' },
{ field: 'ScheduledDate', title: 'Scheduled Date' },
{ field: 'StreetNumber', title: 'ST#', width: '11%' },
{ field: 'StreetName', title: 'Street Name' },
{ field: 'City', title: 'City' },
{ field: 'State', title: 'ST.' },
{ field: 'IsClaimed', title: 'Claimed'}
],
dataSource: serviceOrderListDataSource
});
我希望能夠使用onChange函數中的值:
var dataItem = grid.dataItem(selectedRow);
在以下視圖中。
ESRIMapView.cshtml:
<body class="claro">
<div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer"
data-dojo-props="design:'sidebar', gutters:false"
style="width:100%; height:100%;">
<div id="leftPane"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'left'">
<br>
<textarea type="text" id="address" />*THIS IS WHERE I WILL USE dataItem! dataItem.StreetNumber (syntax?) to be exact</textArea>
<br>
<button id="locate" data-dojo-type="dijit/form/Button">Locate</button>
</div>
<div id="map"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'center'">
</div>
</div>
</body>
現在,當用戶單擊index.cshtml屏幕上的按鈕時,我的ESRIMapView已加載,該屏幕包含我試圖從中獲取值的網格。
<li>@Html.ActionLink("Map", "ESRIMapView", "Home", null, new { @class = "k-button" })</li>
這是我的“家庭”控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Services.Description;
using Alliance.MFS.Data.Client.Models;
using Alliance.MFS.Data.Local.Repositories;
namespace AllianceMobileWeb.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult ServiceOrderMaintenance()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult ESRIMapView()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
我意識到這可能是一個非常基本的問題,但是可以提供任何幫助。 並且請盡可能詳細地回答:)
由於您是在將(初始)視圖返回給用戶之前創建鏈接的,因此需要一些技巧來更改它。 我提出以下建議:您在設置一個id a
元素並改變其href
屬性; 在您的控制器上,設置與街道編號相對應的參數,並預先填充視圖:
控制器:
public ActionResult ESRIMapView(string streetNumber)
{
ViewBag.Message = "Your contact page.";
ViewBag.StreetNumber = streetNumber;
return View();
}
包含li
視圖(注意a
元素上的ID):
<li>@Html.ActionLink("Map", "ESRIMapView", "Home", null, new { @class = "k-button", id="myMapaction" })</li>
包含textarea
視圖(ESRIMapView):
<textarea type="text" id="address" />@ViewBag.StreetNumber</textArea>
grid.js:
function onChange(e) {
//get currently selected dataItem
var grid = e.sender;
var selectedRow = grid.select();
var dataItem = grid.dataItem(selectedRow);
//change the link
var actionElem = $("#myMapaction");
var url = actionElem.attr("href");
if (url.indexOf("=") === -1) { //first time selecting a row
url += "?streetNumber=" + dataItem.StreetNumber;
} else {
url = url.substring(0, url.lastIndexOf("=") +1) + dataItem.StreetNumber;
}
actionElem.attr("href", url);
//change the link
var y = $.ajax({
url: "/api/ServiceOrderData/" + dataItem.id,
type: 'GET',
dataType: 'json'
});
}
該腳本僅在查詢字符串中添加街道編號參數。 當用戶第一次選擇行時,查詢字符串中不存在streetNumber
參數。 第一次之后,參數就在那里了,我們只能更改值。
請注意,這個解決方案也有其局限性:如果您在查詢字符串其他參數(添加/編輯參數的邏輯必須改變)這是行不通的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.