dataTable.editable; addData函数无法正常工作

时间:2013-08-22 07:42:23

标签: jquery datatables

在我的数据库中,我有顾问表,其中包含顾问ID和ConsultantName,另一张表每周营业时间,其中顾问ID有FK,每个顾问在一周内存储营业时间。我在客户端模式下使用jQuery DataTables Editable插件来显示一个包含consultantName,week,hours,billableRateRt等的表。 该表的内联编辑功能完美无缺,但当我向表中添加新行时,我收到此错误

DataTables警告(table id ='myDataTable'):从数据源请求未知参数'0'

新行将添加到每周小时表中。我认为错误是因为顾问名称不属于weeklyHours的一部分。我通过加入weeklyhours和顾问表获得顾问名称。所以我将以下属性添加到我的weeklyhour模型类中,以便我可以在添加新行之前在addData函数中设置此属性

[NotMapped] public string ConsultantName { get; set; }

以下是addData函数

 public int AddData(string ConsultantName, string week, string hours, string BillableRateRT, string BillableRateOT, string PayableRateRT, string PayableRateOT, string currency)
   {

       DateTime wek = Convert.ToDateTime(week);
       var cc=db.Consultants.First(d => d.ConsultantName.ToLower().Equals(ConsultantName.ToLower())).ConsultantID; 
       var consu = from ww in db.WeeklyHours
                   where (ww.ConsultantID == cc && (ww.Week.Year == wek.Year && ww.Week.Month == wek.Month && ww.Week.Day == wek.Day))
                   select ww.ConsultantID;
       if (consu.Any())
       {
           Response.Write(ConsultantName + " work hours for"  + week + "' already exists");
           Response.StatusCode = 404;
           Response.End();
           return -1;

       }

       var weeklydata = from q in db.WeeklyHours
                        select q;
       var wh = new WeeklyHour();
       wh.ConsultantID = cc;

       wh.ConsultantName = ConsultantName;

       wh.Week = Convert.ToDateTime(week);
       wh.Hours = Convert.ToInt32(hours);
       wh.BillableRateRT = Convert.ToDecimal(BillableRateRT);
       wh.BillableRateOT = Convert.ToDecimal(BillableRateOT);
       wh.PayableRateRT = Convert.ToDecimal(PayableRateRT);
       wh.PayableRateOT = Convert.ToDecimal(PayableRateOT);
       wh.Currency = currency;
       db.WeeklyHours.Add(wh);
       try
       {
           db.SaveChanges();
       }
       catch (Exception e)
       {
           Console.WriteLine(e);
           // Provide for exceptions.
       }

       return wh.ConsultantID;
   }

每次添加一行时,我仍然会收到同样的错误。此外,我认为当我刷新表时,我看到顾问名称字段具有正确的数据。

以下是初始化Datatable

的代码
`<script language="javascript" type="text/javascript">
$(document).ready(function () {
    $('#myDataTable').dataTable().makeEditable({
        sUpdateURL: "/DataEntry/UpdateData",
      sAddURL: "/DataEntry/AddData"

    });

});

    <div id="formSelection">
<table id="myDataTable" class="display">
    <thead>
        <tr>

            <th>
                ConsultantName
            </th>
            <th>
                Week
            </th>
            <th>
                Hours
            </th>
            <th>
                BillableRateRT
            </th>
            <th>
                BillableRateOT
            </th>
            <th>
                PayableRateRT
            </th>
            <th>
                PayableRateOT
            </th>
            <th>
                Currency
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr id="@item.ConsultantID">

                <td>@item.ConsultantName
                </td>
                <td>@item.Week.ToShortDateString() @*to get date only*@
                </td>
                <td>@item.Hours
                </td>
                <td>@item.BillableRateRT
                </td>
                <td>@item.BillableRateOT
                </td>
                <td>@item.PayableRateRT
                </td>
                <td>@item.PayableRateOT
                </td>
                 <td>@item.Currency
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="add_delete_toolbar" />`

以下是创建添加数据表单的代码

   @{
   IQueryable<string> ConsultantNameList = DropdownUitility.getConsultantsforCompany(Model);

}

    <form id="formAddNewRow" action="#" title="Add Weekly Hours for a Consultant">
 <label for="name">ConsultantName</label>
  <select name="ConsultantName" id="ConsultantName">
   @{
     int count = 0;
    // String name = null;
     foreach (var name in ConsultantNameList)
     {
         count++;

       <option value= "@name.ToString()" id="@count" rel="0">@name </option>
     }
   }`</select>
 <br />
   <label for="name">
   Week</label><input type="text" name="Week" id="Week" rel="1" />
  <br />
   <label for="name">
    Hours</label><input type="text" name="Hours" id="Hours" rel="2" />
  <br />
    <label for="name">
   BillableRateRT</label><input type="text" name="BillableRateRT" id="BillableRateRT"
       rel="3" />
  <br />
   <label for="name">
   BillableRateOT</label><input type="text" name="BillableRateOT" id="BillableRateOT"
       rel="4" />
   <br />
   <label for="name">
   PayableRateRT</label><input type="text" name="PayableRateRT" id="PayableRateRT" rel="5" />
    <br />
   <label for="name">
   PayableRateOT</label><input type="text" name="PayableRateOT" id="PayableRateOT" rel="6" />
   <br />
   <label for="name">
   Currency</label><input type="text" name="Currency" id="Currency" rel="7" />
   <br />
   <br />
    </form>` 

1 个答案:

答案 0 :(得分:0)

我发现了这个bug,它是ConsultantNames的html selectList中的rel属性。

错误:

  <select name="ConsultantName" id="ConsultantName">
   @{
     int count = 0;
     foreach (var name in ConsultantNameList)
     {
         count++;

       <option value= "@name.ToString()" id="@count" rel="0">@name </option>
     }
   }
  </select>

select应具有可编辑插件用于将数据与列匹配的rel属性。在我之前的代码中,我将rel属性放在Options标记中,这是错误的。

正确:

     <select name="ConsultantName" id="ConsultantName" rel="0">
   @{
     int count = 0;
     foreach (var name in ConsultantNameList)
     {
         count++;

       <option value= "@name.ToString()" id="@count" >@name </option>
     }
   }
    </select>