|
Index.cshtml
<ejs-tab id="ej2Tab" heightAdjustMode="None" height="390" created="tabCreated" selecting="tabSelecting">
<e-tab-tabitems>
<e-tab-tabitem header="ViewBag.headerTextOne" content="@content1"></e-tab-tabitem>
<e-tab-tabitem header="ViewBag.headerTextTwo" content="@content2" disabled="true"></e-tab-tabitem>
</e-tab-tabitems>
</ejs-tab>
<script>
function tabNavigations(args) {
switch (args.target.id) {
case "searchNext":
if (….)
{
….
}
else {
var item1 = startPoint.text; // Get selected value
var item2 = endPoint.text;
var item3 = Math.floor((Math.random() * 3) + 2);
$.ajax({
type: "POST",
url: "/Home/OnPostSend", // url
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify({ // Passed data into serverside
Item1: item1,
Item2: item2,
Item3: item3
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
availTrainGrid.dataSource = response;// Get JSON data and assigned into grid
tabObj.enableTab(0, false); // Disable first tab
tabObj.enableTab(1, true); // Enable first tab
tabObj.select(1); // Select first tab
document.getElementById("err1").innerText = "";
document.getElementById("err2").innerText = "";
},
});
}
}
break;
case "book":
$.ajax({
type: "POST",
url: "/Home/OnDialogSend", // Define server side url
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify({ gridData: selectedTrain }), // Passed selected data into server side
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var data = JSON.parse(response); // Convert string into JSON object
// Set dialog content
if (!ej.base.isNullOrUndefined(data.gridData) && !ej.base.isNullOrUndefined(data.gridData.trainNo))
alertDlg.content = '<div>You have selected the train on <table><tr><td><b>Train No: <b></td><td>' + data.gridData.trainNo + '</td></tr>table></div>';
else
alertDlg.content = 'You need to select any one train';
alertDlg.show();
selectedTrain = [];
}
});
Break;
}
}
</script> |
|
HomeController.cs
public class PostData
{
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
}
public class DialogData
{
public string gridData { get; set; }
}
public class Trains
{
public int TrainNo { get; set; }
public string Name { get; set; }
public string Departure { get; set; }
public string Arrival { get; set; }
public int Availability { get; set; }
}
public ActionResult OnPostSend() // Triggered when click first tab button
{
string sPostValue1 = "";
string sPostValue2 = "";
string sPostValue3 = "";
{
MemoryStream stream = new MemoryStream();
Request.Body.CopyTo(stream);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
string requestBody = reader.ReadToEnd();
if (requestBody.Length > 0)
{
var obj = JsonConvert.DeserializeObject<PostData>(requestBody);
if (obj != null)
{
sPostValue1 = obj.Item1;
sPostValue2 = obj.Item2;
sPostValue3 = obj.Item3;
}
}
}
}
int m = Int32.Parse(sPostValue3);
Random rnd = new Random();
for(int i=0; i < m;i++)
{
train.Add(new Trains { TrainNo = ((rnd.Next() * 20) + 19000), Name= "Train " + i, Departure=sPostValue1, Arrival=sPostValue2, Availability= ((rnd.Next() * 20) + 20)
});
}
return new JsonResult(train);
}
public ActionResult OnDialogSend() // Triggered when second tab book button
{
string value1 = "";
{
MemoryStream stream = new MemoryStream();
Request.Body.CopyTo(stream);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
string requestBody = reader.ReadToEnd();
if (requestBody.Length > 0)
{
var obj = (requestBody);
if (obj != null)
{
value1 = obj;
}
}
}
}
List<string> lstString = new List<string>{
value1
};
return new JsonResult(value1);
}
|