new to MVC and to you Grid control, sorry for silly questions and thanks in advance for any help.
Edit is working but don't understand how to return back Master data to the view after insert, it should return with id keys compiled, as they are assigned server side.
Master/Details entities are:
Recipe (only Description field) and
Ingredients (Description, Calories, Fat, Carb., ...), data is on Azure Table Storage, ids are PartitionKey/RowKey determined server side.
RecipeController has a common CreateEdit.vbhtml view, presenting both entities data (Recipe description plus Ingredients grid) with hidden pk/rk as they don't make sense to the user (GUID/UtcNow.Ticks...)
I can separate Create and Edit views if needed...
CreateEdit view is called from main Index, it is explicitly bound to Master/Recipe model:
@ModelType Recipe
Presenting data through separate Forms, Master/Recipe:
<div>
@Using Html.BeginForm
@<fieldset>
<legend>RECIPE</legend>
<p>
@Html.HiddenFor(Function(Model) Model.PartitionKey)
@Html.HiddenFor(Function(Model) Model.RowKey)
</p>
<p>
@Html.LabelFor(Function(Model) Model.Description)
@Html.EJ().MaskEditTextBoxFor(Function(Model) Model.Description).MaskFormat("").InputMode(InputMode.Text)
</p>
</fieldset>
End Using
</div>
And Details/Ingredients grid:
<div>
@Using Html.BeginForm
@<fieldset>
<legend>INGREDIENTS</legend>
<div class="col-md-9 ptiles">
@Html.EJ().Grid(Of Ingredient)("Grid").Datasource......
................
ds.URL(Url.Action("
IngredientsDataSource", "Recipe", Nothing, Request.Url.Scheme)).BatchURL(Url.Content("
BatchSave")).Adaptor(
AdaptorType.UrlAdaptor)
.................
.EditSettings......
ep.AllowEditing(True).AllowAdding(True).AllowDeleting(True).EditMode(EditMode.Batch).ShowConfirmDialog()
......................................
.Columns(Sub(column)
column.Field(Function(obj) (obj.PartitionKey)).HeaderText(Html.DisplayNameFor(Function(obj) (obj.PartitionKey)).ToString).IsPrimaryKey(True).Visible(False).Add()
column.Field(Function(obj) (obj.RowKey)).HeaderText(Html.DisplayNameFor(Function(obj) (obj.RowKey)).ToString).IsPrimaryKey(True).Visible(False).Add()
column.Field(Function(obj) (obj.Description)).Type("string").EditType(EditingType.String).Add()
column.Field(Function(obj) (obj.Calories)).Type("number").EditType(EditingType.Numeric).DefaultValue(0).Add()
...........
.ClientSideEvents(Function(events) (events.ActionBegin("actionBegin").ActionComplete("actionComplete")))
</div>
</fieldset>
End Using
</div>
Following your suggestions from an older post I'm appending Master form value before sending grid form value to controller, intercepting ajaxSend.
By the way, i'm
forcing url parameter passing pk/rk to "IngredientsDataSource" but I guess this is the wrong place??$(document).ajaxSend(function (event, xhr, opt) {
var description = $("#Description").ejMaskEdit("get_StrippedValue");
var data = JSON.parse(opt.data);
data.Form = [{ PartitionKey: '@Model.PartitionKey', RowKey: '@Model.RowKey', Description: description }];
//
This is not needed for BatchSave controller method, as it receives master form data...
opt.url += "?pk=" + '@Model.PartitionKey' + "&rkRecipe=" + '@Model.RowKey';
opt.data = JSON.stringify(data)
})
Controller's datasource:
Function
IngredientsDataSource(ByVal pk As String, rkRecipe As String) As ActionResult
Dim list As New List(Of Ingredient)
If Not String.IsNullOrEmpty(rkRecipe) Then
'Reading ingredients children matching with Recipe pk/rk...
'...........................
End If
Dim result As New BatchDataResult
result.result = list
result.count = list.Count
Return Json(result, JsonRequestBehavior.AllowGet)
End Function
Controller's batch save:
<HttpPost()>
Function
BatchSave(ByVal changed As List(Of Ingredient), ByVal added As List(Of Ingredient),
ByVal deleted As List(Of Ingredient),
ByVal Form As List(Of Recipe)) As ActionResult
Try
'CRUD works fine, business logic sets new pk/rk and both entities are correctly updated/created
............
Dim list = db.Table("Recipe").ExecuteQuery(Of Ingredient)(query).ToList
Return Json(list, JsonRequestBehavior.AllowGet)
Catch ex As Exception
Return Nothing
End Try
End Function
returning from BatchSave after creating a new Recipe and its Ingredients the view still has old Master/Recipe model, still without pk/rk...
I'd like not to use ViewData/ViewBag objects if possible as view is already bound to Model...
Not sure if what I'm doing, asking and assumig makes sense...any suggestion will be much appreciated!