I am struggling to understand how to handle create operations when ListView contents must be associated with a parent object. To demonstrate by using examples from your docs, let's say the orders are part of a store object:
public class StoreViewModel
{
public int StoreID {get; set;}
IEnumerable<OrderViewModel> StoreOrders {get; set;}
// various other fields
}
My goal is to have a View, Model type is the StoreViewModel, and a ListView showing the Orders from a specific store. The Create function of the ListView would need to know the store ID and have it passed somehow. I think I need something like this:
@model StoreViewModel
@(Html.Kendo().ListView<OrderViewModel>()
.DataSource(ds => ds
.Ajax()
.Model(m => m.("OrderID")
.Read(read => read
.Action("Orders_Read", "ListView")
.Data(@<text>@Html.Raw(Json.Serialize(Model))</text>)
)
.Create(create => create
.Action("Orders_Create", "ListView")
.Data(@<text>@Html.Raw(Json.Serialize(Model))</text>)
)
)
)
The controller has this method for read, which works perfectly:
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request, StoreViewModel store)
{
// code that returns orders from one store
}
However, when I try to create, the same .Data parameter doesn't seem to be passed:
public ActionResult Orders_Create([DataSourceRequest]DataSourceRequest request, StoreViewModel store)
{
// misc code to make a new order and add to DB goes here
newOrder.StoreID = store.StoreID; // this fails
}
When looking through this with the debugger, "store" is populated in the Orders_Read method, but is null in the Orders_Create method, even though I'm trying to use them the same way. While that's concerning, I suspect there's an overall better way to do this period. Are there any examples that show similar scenarios with related models/objects?
Another thing I tried today - using the .Filter() method. I was hoping I could filter by store ID then see this via the DataSourceRequest object within the controller:
.Filter(filters =>
{
filters.Add(s => s.StoreID).Equals(Model.StoreID);
})
This *appears* to work initially, but when following along in the debugger, Orders_Read, I look at the request.Filters[0] and find that the Member and Operator are populated correctly, but Value is null. This causes an exception in ToDataSourceResult(). I'm not sure why the filter value wouldn't be populated. Even removing Model.StoreID and hard coding an int like 2 doesn't work. No idea why the filter is broken.