我尝试在窗口加载div中渲染一些对象..事实上该对象已通过,但我在函数中无法完成工作..我无法做到添加一个onclick方法,也不格式化一些字符串..(我检查this但我能够像我打算在同一个文件中的其他函数那样格式化同一个对象..)
我像这样序列化我的对象
[HttpGet]
public string GetCartItems(string customerId)
{
var items = _context.Carts
.Include(b => b.Item)
.Where(b => b.Item.Status == BookingStatus.Available && b.CustomerId == customerId).ToList()
.Select(b => b.Item.ToCalendarEvent()).ToList();
return JsonConvert.SerializeObject(items);
}
在我的js文件中,我在加载
时调用此函数function renderCartItems() {
//this function renders all the cart elements into the cart in home/mentor
const customerId = $("#customerId").html();
//ensure all html elements in the cart are cleared
var $div = $('#mentorItems');
while ($div.firstChild) {
$div.removeChild($div.firstChild);
}
//render all the events as html in the cart
$.getJSON(`/Cart/GetCartItems?customerId=${customerId}`,
function (data) {
data.forEach(function(e) {
addEventToCartHtml(e);
});
});
}
addEventToCartHtml函数:
function addEventToCartHtml(event) {
const name = $("#mentorName").html();
const price = $("#mentorHourly").html();
const customerId = $("#customerId").html();
var start = event.start.format("MM/DD: HH:mm");
var end = event.end.format("MM/DD: HH:mm");
var $div = $('<div id="' + event.id + '" class="alert alert-dismissable"></div>');
var $btn = $('<button class="close" data-dismiss="alert">×</button>');
var $name = $('<p> ' + name + ' - 1 time</p>');
var $time = $('<p class="time"> ' + start + ' - ' + end + '</p>');
var $hourly = $('<p>Timepris: ' + price + ' kr.</p>');
$btn.on('click', function () { removeEvent(event); });
$div.append($btn);
$div.append($name);
$div.append($time);
$div.append($hourly);
$('#mentorItems').append($div);
//mark the vent as grey
event.color = "grey";
updateEvent(event);
}
我已尝试在Chrome中调试它并且事件确实传递给了对象..但是说event.start.format不是已定义的函数..
解决方案:
我正在对我的对象进行双重序列化,因此我按照&#34; dbc&#34;评论中的说明进行操作!
我还格式化了我的日期字符串:
start = moment(event.start, 'YYYY-MM - DD[T]HH: mm: ss').format('DD/MM: HH:mm');