This is a migrated thread and some comments may be shown as answers.

Insert Child Record in Detail Grid

0 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lyle
Top achievements
Rank 1
Lyle asked on 02 Dec 2010, 08:41 AM
Hello,
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 => {
    dataBinding.Ajax()
        .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>" %>
  
<ul>
    <li>
        <%= 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>
            <% }; %>
        </select>
    </li>
    <li>
        <%= Html.LabelFor(x => x.UserName) %>
        <%= Html.TextBox("UserName", Model.UserName, Model.EmployeeProgramId >0 ? new { @readonly "readonly" } : null)%>
    </li>
    <li>
        <%= Html.LabelFor(x => x.Password) %>
        <%= Html.TextBox("Password", Model.Password, new {style="width:10em" }) %>
    </li>
    <li>
        <%= Html.LabelFor(x => x.EffectiveDate) %>
        <%= Html.Telerik().DatePicker()
    .Name("EffectiveDate")
    .MinDate(new DateTime(2000, 1, 1))
    .MaxDate(new DateTime(2019, 12, 31))
    .ShowButton(true)
    .Value(Model.EffectiveDate)
    %>
    </li>
    <li>
        <%= Html.LabelFor(x => x.ExpirationDate) %>
        <%= Html.Telerik().DatePicker()
    .Name("ExpirationDate")
    .ShowButton(false)
    .Enable(false)
    .Value(Model.ExpirationDate)    
        %>
    </li>
</ul>
<%= Html.TextBox("EmployeeProgramId", Model.EmployeeProgramId, new { style = "visibility:hidden" }) %>
<%= Html.TextBox("EmployeeId", Request.QueryString["EmployeeId"], new { style = "visibility:hidden" }) %>

and finally the Controller:
[GridAction]
[AcceptVerbs(HttpVerbs.Post)]
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));
            //  };
            //};
  
            this.epmdc.EmployeePrograms.InsertOnSubmit(ep);
            this.epmdc.SubmitChanges();
        };
    };
    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!

LL
Tags
Grid
Asked by
Lyle
Top achievements
Rank 1
Share this question
or