Hello registrator,
Let me give you a simple example that covers both your questions. The example's Model models a one to many relationship between a Branch (a company branch, a bank branch etc) and its Customers. Sample data are also included.
Example Model:
public class Branch
{
public int ID { get; set; }
public string Name { get; set; }
public List<Customer> Customers { get; set; }
public static List<Branch> GetAll()
{
return new List<Branch>()
{
new Branch
{
ID = 1,
Name = "Town 1 branch",
Customers = new List<Customer>
{
new Customer
{
ID = 100,
Name = "Customer 100",
Phone = "100 123456"
},
new Customer
{
ID = 101,
Name = "Customer 101",
Phone = "101 123456"
},
new Customer
{
ID = 102,
Name = "Customer 102",
Phone = "102 123456"
}
}
},
new Branch
{
ID = 2,
Name = "Town 2 branch",
Customers = new List<Customer>
{
new Customer
{
ID = 200,
Name = "Customer 200",
Phone = "200 123456"
},
new Customer
{
ID = 201,
Name = "Customer 201",
Phone = "201 123456"
}
}
}
};
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
Example Controller:
public class OneToManyController : Controller
{
public ActionResult Index()
{
return View(Branch.GetAll()[0]);
}
}
Example View:
@model Branch
@{
Layout = null;
var X = Html.X();
}
<!DOCTYPE html>
<html>
<head>
<title>MVC GridPanels</title>
<script>
var CustomerSelector = {
add: function (source, destination) {
source = source || App.GridPanel1;
destination = destination || App.GridPanel2;
if (source.getSelectionModel().hasSelection()) {
var records = source.getSelectionModel().getSelection();
source.store.remove(records);
destination.store.add(records);
}
}
};
</script>
</head>
<body>
@X.ResourceManager()
@(X.FormPanelFor(m => m)
.BodyPadding(5)
.DefaultAnchor("100%")
.Width(350)
)
@(X.GridPanelFor(m => m.Customers,false, true, false)
.ID("GridPanel1")
.Width(350)
.Height(250)
.ColumnModel(
X.Column().DataIndex("ID").Hidden(true),
X.Column().Text("Name").DataIndex("Name").Flex(1),
X.Column().Text("Phone").DataIndex("Phone").Width(150)
)
.SelectionModel(X.RowSelectionModel().Mode(SelectionMode.Multi))
.Buttons(
X.Button()
.Icon(Icon.ResultsetNext)
.Listeners(l =>
l.Click.Handler = "CustomerSelector.add();"
)
)
)
@(X.GridPanel()
.ID("GridPanel2")
.Width(350)
.Height(250)
.Store(X.Store()
.Model(X.Model()
.Fields("ID","Name","Phone")
)
)
.ColumnModel(
X.Column().DataIndex("ID").Hidden(true),
X.Column().Text("Name").DataIndex("Name").Flex(1),
X.Column().Text("Phone").DataIndex("Phone").Width(150)
)
)
</body>
</html>
View lines 30 to 44 answer your first question about how to bind the model to the view. There is nothing special here apart from line 36 where the
GridPanel
is bound to the list of Customers. I am pretty sure you have figured it out yourself, already.
View lines 13 to 25, 45 to 52 and 55 to 70 answer your second question about how to move records from one
GridPanel
to another. It is important to note that the first grid defines its
SelectionModel
. It is also important to see how the
source
and
destination
javascript variables are assigned to references of the two gridpanels. I have intentionally highlighted the most important lines to help you correct these specific parts of your code.
Hope it helps.