Im trying to create a master / detail create page in mvc 5
at the top I have normal text input fields
at the buttom I have a grid
when I post back the data I entered in the grid is not there
the model look like this
public Bestiller bestilleren { get; set; }public IQueryable<Kontaktperson> kontaktpersoner { get; set; }
and the grid :
@(Html.Infragistics().Grid(Model.kontaktpersoner).PrimaryKey("ID").ID("grdKontaktpersoner").ShowHeader(true).Columns(column =>{column.For(m => m.ID).Hidden(true);column.For(m => m.Navn).HeaderText("Navn").Width("200px");column.For(m => m.E_Mail).HeaderText("Email").Width("200px");column.For(m => m.Tlfnr).HeaderText("Telefonnr.").Width("80px");column.For(m => m.RefNr).HeaderText("Ref. Nr.").Width("100px");}).Features(feature =>{feature.Updating().ColumnSettings(cs =>{cs.ColumnSetting().ColumnKey("ID").ReadOnly(true);cs.ColumnSetting().ColumnKey("Navn").EditorType(ColumnEditorType.Text).Required(true).TextEditorOptions(o => o.ValidatorOptions(vo => vo.MaxLength(50)));cs.ColumnSetting().ColumnKey("E_Mail").EditorType(ColumnEditorType.Text).TextEditorOptions(o => .ValidatorOptions(vo => vo.MaxLength(60)));cs.ColumnSetting().ColumnKey("Tlfnr").EditorType(ColumnEditorType.Text).TextEditorOptions(o => o.ValidatorOptionsvo => vo.MaxLength(20)));cs.ColumnSetting().ColumnKey("RefNr").EditorType(ColumnEditorType.Text).TextEditorOptions(o => o.ValidatorOptionsvo => vo.MaxLength(30)));}).CancelLabel("Afbryd").AddRowLabel("Tilføj kontaktperson").DeleteRowTooltip("Slet kontaktperson").DoneLabel("Gem").EditMode(GridEditMode.Row).EnableDeleteRow(true);}).AutoGenerateColumns(false).AutoAdjustHeight(true).AutoCommit(true).Width("100%").DataSource(Model.kontaktpersoner).DataBind().Render())
I use 2014.1 sr 2249
Hi Christian,
I see you have the DataSource() set but I believe you probably want to set the DataSourceURL instead. Also I don't see where you are setting the UpdateUrl. You will need this set to the UpdateUrl so the controller can update the model.
I have attached an example you can reference. Here within you will see:
@(Html.Infragistics()
.Grid<MvcApplication1.Models.Wine>()
.ID("grid1")
.AutoGenerateLayouts(false)
.Columns(column =>
{
column.For(x => x.WineID).HeaderText("ID").DataType("number");
column.For(x => x.ProductName).HeaderText("ProductName").DataType("string");
column.For(x => x.Region).HeaderText("Region").DataType("string");
column.For(x => x.VintageYear).DataType("date").HeaderText("Vintage Year");
column.For(x => x.CategoryID).DataType("int").HeaderText("Category ID");
column.For(x => x.AverageUnitPricePerBottle).DataType("decimal").HeaderText("Avg Price");
column.For(x => x.RecommendToFriend).DataType("bool").HeaderText("Recommend Wine");
column.For(x => x.TroyRating).DataType("int").HeaderText("TroyRating");
})
.Width("800px")
.Height("500px")
.PrimaryKey("WineID")
.Features(features =>
features.Sorting().Type(OpType.Local).ColumnSettings(settings =>
settings.ColumnSetting().ColumnKey("WineID").CurrentSortDirection("ascending");
});
features.Selection().Mode(SelectionMode.Row).MultipleSelection(true).Activation(true);
features.Paging().Type(OpType.Local).PageSize(5);
features.Updating().EditMode(GridEditMode.Row).ColumnSettings(cs =>
cs.ColumnSetting().ColumnKey("WineID").ReadOnly(false);
cs.ColumnSetting().ColumnKey("VintageYear").EditorType(ColumnEditorType.DatePicker);
.DataSourceUrl(Url.Action("GetGridData", "Home"))
.UpdateUrl(Url.Action("UpdateGrid", "Home"))
.DataBind()
.Render())
Then take a look at what I am doing in my controller.
Let me know if you have any additional questions related to this inquiry. Thanks Christian!
This is a create page meaning that both Bestiller and Kontaktpersoner are empty
I cant update data to the server on Kontaktperson when the foreign key Bestiller_ID is empty
Can you take my example (attached to this thread) and update it to reflect your scenario? Then send it back to me. It will be helpful to have a small running example of your scenario that I can use as part of my research into this matter.
Thanks!
I have attached a sample
Thank you for your sample. I believe all you need to do is set Bestiller_ID to be nullable. You can do this by using the nullable modifier when setting the property in your model.
Once you allow Bestiller_ID to accept a null value it should be possible to then update the data source.
I change the grid to this
@{
var data = Model.Kontaktperson.AsQueryable();
}
@(Html.Infragistics().Grid<intranet.mvc5.Models.Kontaktperson>(data)
.PrimaryKey("ID")
.ID("grdKontaktpersoner")
.ShowHeader(true)
column.For(m => m.ID).Hidden(true);
column.For(m => m.Navn).HeaderText("Navn").Width("200px");
column.For(m => m.E_Mail).HeaderText("Email").Width("200px");
column.For(m => m.Tlfnr).HeaderText("Telefonnr.").Width("80px");
column.For(m => m.RefNr).HeaderText("Ref. Nr.").Width("100px");
.Features(feature =>
feature.Updating().ColumnSettings(cs =>
cs.ColumnSetting().ColumnKey("ID").ReadOnly(true);
cs.ColumnSetting().ColumnKey("Navn").EditorType(ColumnEditorType.Text).Required(true).TextEditorOptions(o => o.ValidatorOptions(vo => vo.MaxLength(50)));
cs.ColumnSetting().ColumnKey("E_Mail").EditorType(ColumnEditorType.Text).TextEditorOptions(o => o.ValidatorOptions(vo => vo.MaxLength(60)));
cs.ColumnSetting().ColumnKey("Tlfnr").EditorType(ColumnEditorType.Text).TextEditorOptions(o => o.ValidatorOptions(vo => vo.MaxLength(20)));
cs.ColumnSetting().ColumnKey("RefNr").EditorType(ColumnEditorType.Text).TextEditorOptions(o => o.ValidatorOptions(vo => vo.MaxLength(30)));
.CancelLabel("Afbryd")
.AddRowLabel("Tilføj kontaktperson")
.DeleteRowTooltip("Slet kontaktperson")
.DoneLabel("Gem")
.EditMode(GridEditMode.Row)
.EnableAddRow(true)
.EnableDeleteRow(true);
.AutoGenerateColumns(false)
.AutoAdjustHeight(true)
.AutoCommit(true)
.Width("100%")
.DataSource(data)
.UpdateUrl(Url.Action("KontaktPersonGem"))
.Render()
)
model intranet.mvc5.Models.Bestiller
KontaktPersonGem never get called
I am still following this thread. Let me know if you need additional information. Thanks!
There are a couple of things that I notice. The first is in your view where you set your UpdateUrl, I see you call KontaktPersonGem, but you don't seem to tell the updateurl where to find KontaktPersonGem. The UpdateUrl should look something like this:
.UpdateUrl(Url.Action("KontaktPersonGem", "Home"))
Also I see you are calling AutoCommit(true) but you need to also call SaveChanges. So, something like this:
<script type="text/javascript">
function saveChanges() {
$("#grid1").igGrid("saveChanges");
</script>
<input type="button" id="saveChanges" value="Save Changes" onclick="saveChanges()" />
Finally, I don't have access to what you're doing in KontaktPersonGem, but if all is working there fine, the updates should successfully save.
Going with my example provided above, this is what I am doing in my UpdateGrid ActionResult that I pass into my UpdateUrl:
public ActionResult UpdateGrid(string ig_transactions)
//postData = HttpContext.Request.Form["ig_transactions"];
//ArrayList data = (ArrayList)Procurios.Public.JSON.JsonDecode(ig_transactions);
GridModel m = new GridModel();
List<Transaction<Wine>> transactions = m.LoadTransactions<Wine>(HttpContext.Request.Form["ig_transactions"]);
ViewData["GenerateCompactJSONResponse"] = false;
foreach (Transaction<Wine> t in transactions)
switch (t.type)
case "row":
wl.UpdateWine(t.row);
break;
case "newrow":
wl.AddWine(t.row);
case "deleterow":
wl.DeleteWine(Convert.ToInt32(t.rowId));
};
JsonResult result = new JsonResult();
Dictionary<string, bool> response = new Dictionary<string, bool>();
response.Add("Success", true);
result.Data = response;
return result;
Please let me know if you need additional assistance.