On my page I have an input box that the user can type a search term into, and it will return a data entry for it.
Each data entry has a 1-many relationship with another table, which I would like to populate into a grid.
An example is type in a persons name, it returns their 1 address, and many phone numbers (home, mobile, office, etc.)
I have the grid working well enough to display the data, but only if I specify which data to return in the code, which gets returned when the page loads.
index.vbhtml:
<div class="row" id="userLookup">
<input type="text" id="username" name="username" /> <button onclick="getUser()">Get User Info</button>
<div id="userInfo"></div>
</div>
<div class="row">
@Html.EJS().Grid("Grid").DataSource(Function(datamanager As DataManagerBuilder) ({datamanager.Json(ViewBag.DataSource.ToArray()).InsertUrl("/Home/Insert").RemoveUrl("/Home/Delete").UpdateUrl("/Home/Update").Adaptor("RemoteSaveAdaptor")})).Columns(Function(col As Object) {
col.Field("ID").HeaderText("ID").IsPrimaryKey(True).Visible(False).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(),
col.Field("Phone").HeaderText("Phone Num").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(),
col.Field("Type").HeaderText("Phone Type").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add()
}).EditSettings(Function(edit) {edit.AllowAdding(True).AllowEditing(True).AllowDeleting(True).ShowDeleteConfirmDialog(True).Mode(Syncfusion.EJ2.Grids.EditMode.Normal)}).Toolbar(New List(Of String)({"Add", "Edit", "Delete", "Update", "Cancel"})).Render();
</div>
<script>
function getUser() {
var user = $("#username").val();
$.get("/Home/GetUser", { username: user }, function (data) {
var json = "<h3>" + data.Username + "</h3><br><b>Address:</b>" + data.Address + "<br>"
$("#userInfo").html(json);
});
}
</script>
HomeController.vb
Imports Microsoft.AspNetCore.Mvc
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private lines As List(Of PhoneLine)
Function Index() As Mvc.ActionResult
Try
Dim dc As New DataClass("Select * From PhoneLines Where userID = 158368", False)
lines = New List(Of PhoneLine)
For Each r As DataRow In dc.Rows
lines.Add(New PhoneLine (r("ID"), r("PhoneNum"), r("Type")))
Next
ViewBag.DataSource = lines
Catch ex As Exception
End Try
Return View()
End Function
Function GetUser(username As String) As Mvc.ActionResult
Dim output As Mvc.ActionResult = Nothing
Try
Dim sSql As String = "Select * From users Where Username like '" & username & "'"
Dim dataReader As System.Data.SqlClient.SqlDataReader = Reader(sSql)
If dataReader.Read Then
Dim j = New With {
.ID = dataReader.GetValue(0),
.Username = dataReader.GetValue(1),
.Address = dataReader.GetValue(2)
}
output = New JsonResult With {.Data = j, .JsonRequestBehavior = JsonRequestBehavior.AllowGet}
Else
Dim j = New With {
.Error = "User not found."
}
output = New JsonResult With {.Data = j, .JsonRequestBehavior = JsonRequestBehavior.AllowGet}
End If
Catch ex As Exception
Throw ex
End Try
Return output
End Function
How do I fill the grid from the getUser function, or alternately, what is the better way to accomplish this?
Thanks!