This is a migrated thread and some comments may be shown as answers.
Insert Child Record in Detail Grid
0 Answers 39 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Top achievements
Rank 1
Lyle asked on 02 Dec 2010, 08:41 AM
I appended this problem to another thread but I guess it was not similar enough to get an answer.  I actually rewrote most of this code just in an attempt to get it to work.  I have a Master and Detail grid using parts of the examples for the MVC Grid.  The purpose is to have a way of recording our Employee's passwords for any given Program.  There are 3 tables: Employee, Program and EmployeeProgram.  It is very simplistic really.  EmployeeProgram is the child to Employee and Program and contains the primary key EmployeeProgramId and a unique index on EmployeeId and ProgramId.

Listing of the Employees (Master) and their current Programs(Detail) is not a problem.  Editing and Deleteing an Employee's Program works fine.  The problem is inserting.  I can not even count the number of ways I have tried this.

The Grid:
.DataBinding(dataBinding => {
        .Select("_SelectEmployeePrograms", "SWFPasswords", new { EmployeeId = "<#= EmployeeId #>" })
        .Insert("_InsertEmployeeProgram", "SWFPasswords", new { EmployeeId = "<#= EmployeeId #>" })
        .Update("_SaveEmployeeProgram", "SWFPasswords")
        .Delete("_DeleteEmployeeProgram", "SWFPasswords");

The User Control in the EditorTemplates folder:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EmployeeProgramList>" %>
        <%= Html.LabelFor(x => x.ProgramName)%>
        <select name="ProgramId" >
            <% foreach (Program Program in (List<Program>)ViewData["Programs"]) { %>
            <option value="<%=Program.ProgramId %>"<%=App.Selected(Program.ProgramId, Model.ProgramId) %>><%=Program.ProgramName %></option>
            <% }; %>
        <%= Html.LabelFor(x => x.UserName) %>
        <%= Html.TextBox("UserName", Model.UserName, Model.EmployeeProgramId >0 ? new { @readonly "readonly" } : null)%>
        <%= Html.LabelFor(x => x.Password) %>
        <%= Html.TextBox("Password", Model.Password, new {style="width:10em" }) %>
        <%= Html.LabelFor(x => x.EffectiveDate) %>
        <%= Html.Telerik().DatePicker()
    .MinDate(new DateTime(2000, 1, 1))
    .MaxDate(new DateTime(2019, 12, 31))
        <%= Html.LabelFor(x => x.ExpirationDate) %>
        <%= Html.Telerik().DatePicker()
<%= Html.TextBox("EmployeeProgramId", Model.EmployeeProgramId, new { style = "visibility:hidden" }) %>
<%= Html.TextBox("EmployeeId", Request.QueryString["EmployeeId"], new { style = "visibility:hidden" }) %>

and finally the Controller:
public ActionResult _InsertEmployeeProgram(int EmployeeId) {
    EmployeeProgram ep = new EmployeeProgram();
    if (ModelState.IsValid) {
        if (TryUpdateModel(ep)) {
            ep.EmployeeId = EmployeeId;
            //if (ep.Program.ProgramName.Equals("MS Exchange Email")) {
            //  if (ep.EffectiveDate != null) {
            //    ep.ExpirationDate = (ep.EffectiveDate + TimeSpan.FromDays(45));
            //  };
    return View(new GridModel<EmployeeProgramList> {
        Data = GetEmployeePrograms(EmployeeId)

The commented code in the controller allows the program to run otherwise it bugs out on the object instance error.  All this does though, is allow me to create new EmployeeProgram records with an EmployeeId of 0.  I can not figure out how to capture the EmployeeId and update the model prior to saving the record.  I have tried with and without the "[AcceptVerbs(HttpVerbs.Post)]".  Various different ways of creating a new instance of the EmployeeProgram table.  Nada.

I can not tell you how appreciative I would be if someone can guide me on how to create a new detail record!

Asked by
Top achievements
Rank 1
Share this question