我创建了一个 mvc 应用程序来显示特定月份的事件详细信息。
我创建了一个视图,其中包括一个 jquery 表(员工),每一行都有一个 ajax actionlink。当我们单击每一行时,一个局部视图将显示其中包括每个员工的一个完整日历。
在那个日历中,我们为每个员工分配新事件
问题是
该事件为第一位员工成功保存,但是当我们选择任何其他链接(不同的员工)时,特定的日历将加载,但在保存时会显示一个错误,例如
“更新条目时发生错误。有关详细信息,请参阅内部异常。”
我的主视图(表创建)
function loadData(data) {
var tab = $('<table class="myTable"></table>');
var thead = $('<thead></thead>');
thead.append('<th>Id</th><th></th>');
thead.append('<th>Username</th>');
tab.append(thead);
$.each(data, function (i, val) {
trow.append('<td>' + val.empID + '</td>');
trow.append('<td>' +"" + '</td>');
trow.append('<td>' + val.empName + '</td>');
var url = '@Url.Action("test", "Home", new { id = -1 })';
url = url.replace('-1', val.empID);
var anchor = '<a class="myClass" href="' + url + '">Edit</a>'
trow.append('<td>' + anchor + '</td>');
tab.append(trow);
});
$("tr:odd", tab).css('background-color', '#C4C4C4');
$("#AllEmployees").html(tab);
};
$(document).on('click', '.myClass', function (evt) {
evt.preventDefault();
//Code to load your partial view
$('#calendar').load(this.href);
});
部分视图(项目分配发生在这里)
$(document).ready(function () {
//calendar will show
//on button save
$('#btnPopupSave').click(function () {
$('#popupEventForm').hide();
var dataRow = {
'userID': empid,
'proID': "a2",
'year': year,
'start': st,
'end': en
}
$.ajax({
type: 'POST',
url: "/Home/SaveEvent",
data: dataRow,
success: function (response) {
if (response == 'True') {
$('#calendar').fullCalendar('refetchEvents');
alert('New event saved!');
}
else {
alert('Error, could not save event!');
}
}
});
});
});
控制器
public bool SaveEvent(string userID, string proID, string year, string start, string end)
{
return AllottedDetails.AllotEmployee(userID,proID, year, start, end);
}
班级
public static bool AllotEmployee(string userID, string proID, string year, string start, string end)
{
string err;
try
{
EMPAllocationDBEntities ent = new EMPAllocationDBEntities();
ProjectAllocationDetail rec = new ProjectAllocationDetail();
rec.UserId = Int32.Parse(userID);
rec.ProjectId = proID;
rec.Year = year;
rec.StartDate = Convert.ToDateTime(start);
rec.EndDate = Convert.ToDateTime(end);
ent.ProjectAllocationDetails.Add(rec);
ent.SaveChanges();
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}
该代码适用于表中的第一个员工,但如果我们选择任何其他员工并尝试分配项目,则会显示错误。我调试了代码,数据正确地传给了班级,但在插入时会出错。
请帮我.....