在我的数据库中,我有一个具有顾问 ID 和 ConsultantName 的顾问表,另一个具有顾问 ID 的表每周小时数具有 FK 并存储每个顾问在一周内工作的小时数。我在客户端模式下使用 jQuery DataTables Editable 插件来显示带有consultName、week、hours、billableRateRt 等的表格。表格的内联编辑功能运行良好,但是当我向表格添加新行时出现此错误
DataTables 警告(表 id = 'myDataTable'):从数据源请求未知参数“0”
新行将添加到每周小时表中。我认为错误是因为顾问姓名不是每周工作时间的一部分。我通过加入每周工作时间和顾问表来获得顾问名称。所以我在每周小时模型类中添加了以下属性,以便在添加新行之前也可以在 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;
}
每次我添加一行时,我仍然会遇到同样的错误。另外,我认为当我刷新表格时,我看到顾问姓名字段有正确的数据。
以下是初始化数据表的代码
`<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>`