зеркало из
1
0
Форкнуть 0

feat: add Scheduler CRUD example for RazorPages

This commit is contained in:
aevangel 2020-07-13 10:21:23 +03:00
Родитель 7e95bf6fc4
Коммит 47458724e4
4 изменённых файлов: 255 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Telerik.Examples.RazorPages.Models
{
public class DateGreaterThanAttribute : ValidationAttribute
{
private const string DEFAULT_ERROR_MSG_TEMPLATE = "{0} should be after {1}";
public DateGreaterThanAttribute()
{
if (String.IsNullOrEmpty(ErrorMessage))
{
ErrorMessage = DEFAULT_ERROR_MSG_TEMPLATE;
}
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DateTime? date = value != null ? (DateTime?)value : null;
var otherValue = validationContext.ObjectType.GetProperty(OtherField).GetValue(validationContext.ObjectInstance);
DateTime? otherDate = otherValue != null ? (DateTime?)otherValue : null;
if (date.HasValue && otherDate.HasValue && otherDate > date)
{
return new ValidationResult(String.Format(ErrorMessage, validationContext.DisplayName, OtherField));
}
return ValidationResult.Success;
}
public string OtherField { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,61 @@
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Telerik.Examples.RazorPages.Models
{
public class MeetingViewModel : ISchedulerEvent
{
public int MeetingID { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
private DateTime start;
[Required]
public DateTime Start
{
get
{
return start;
}
set
{
start = value.ToUniversalTime();
}
}
public string StartTimezone { get; set; }
private DateTime end;
[Required]
[DateGreaterThan(OtherField = "Start")]
public DateTime End
{
get
{
return end;
}
set
{
end = value.ToUniversalTime();
}
}
public string EndTimezone { get; set; }
public string RecurrenceRule { get; set; }
public int? RecurrenceID { get; set; }
public string RecurrenceException { get; set; }
public bool IsAllDay { get; set; }
public string Timezone { get; set; }
public int? RoomID { get; set; }
public IEnumerable<int> Attendees { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,87 @@
@page
@model Telerik.Examples.RazorPages.Pages.Scheduler.SchedulerCrudOperationsModel
@{
ViewData["Title"] = "SchedulerCrudOperations";
}
@using Kendo.Mvc.UI
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
@(Html.Kendo().Scheduler<Telerik.Examples.RazorPages.Models.MeetingViewModel>()
.Name("scheduler")
.Height(600)
.Views(views =>
{
views.DayView();
views.WeekView(weekView => weekView.Selected(true));
views.MonthView();
views.AgendaView();
})
.Timezone("Etc/UTC")
.Resources(resource =>
{
resource.Add(m => m.RoomID)
.Title("Room")
.DataTextField("Text")
.DataValueField("Value")
.DataColorField("Color")
.BindTo(new[] {
new { Text = "Meeting Room 101", Value = 1, Color = "#6eb3fa" },
new { Text = "Meeting Room 201", Value = 2, Color = "#f58a8a" }
});
resource.Add(m => m.Attendees)
.Title("Attendees")
.Multiple(true)
.DataTextField("Text")
.DataValueField("Value")
.DataColorField("Color")
.BindTo(new[] {
new { Text = "Alex", Value = 1, Color = "#f8a398" },
new { Text = "Bob", Value = 2, Color = "#51a0ed" },
new { Text = "Charlie", Value = 3, Color = "#56ca85" }
});
})
.DataSource(d => d
.Model(m =>
{
m.Id(f => f.MeetingID);
m.Field(f => f.Title).DefaultValue("No title");
m.Field(f => f.Attendees).DefaultValue(1);
m.RecurrenceId(f => f.RecurrenceID);
})
.Events(e => e.Error("error_handler"))
.Read(read=>read.Url(Url.Page("SchedulerCrudOperations","Meetings_Read")).Data("forgeryToken"))
.Create(create=>create.Url(Url.Page("SchedulerCrudOperations","Meetings_Create")).Data("forgeryToken"))
.Destroy(destroy=>destroy.Url(Url.Page("SchedulerCrudOperations","Meetings_Destroy")).Data("forgeryToken"))
.Update(u=>u.Url(Url.Page("SchedulerCrudOperations","Meetings_Update")).Data("forgeryToken"))
)
)
<script>
function forgeryToken() {
return kendo.antiForgeryTokens();
}
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
var scheduler = $("#scheduler").data("kendoScheduler");
scheduler.one("dataBinding", function (e) {
//prevent saving if server error is thrown
e.preventDefault();
})
}
}
</script>

Просмотреть файл

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Telerik.Examples.RazorPages.Models;
namespace Telerik.Examples.RazorPages.Pages.Scheduler
{
public class SchedulerCrudOperationsModel : PageModel
{
public static IList<MeetingViewModel> meetings;
public void OnGet()
{
if (meetings == null)
{
meetings = new List<MeetingViewModel>();
Enumerable.Range(1, 5).ToList().ForEach(x => meetings.Add(new MeetingViewModel()
{
MeetingID = x,
Title = "Event " + x,
Start = DateTime.Now.AddHours(x * 2),
End = DateTime.Now.AddHours(x * 3),
Description = "Description for event " + x,
Attendees = new List<int>() { (x % 3) +1 }
})); ;
}
}
public virtual JsonResult OnPostMeetings_Read([DataSourceRequest] DataSourceRequest request)
{
return new JsonResult(meetings.ToDataSourceResult(request));
}
public virtual JsonResult OnPostMeetings_Destroy([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting)
{
if (ModelState.IsValid)
{
meetings.Remove(meetings.First(x => x.MeetingID == meeting.MeetingID));
}
return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState));
}
public virtual JsonResult OnPostMeetings_Create([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting)
{
if (ModelState.IsValid)
{
meeting.MeetingID = meetings.Count + 2;
meetings.Add(meeting);
}
return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState));
}
public virtual JsonResult OnPostMeetings_Update([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting)
{
if (ModelState.IsValid)
{
meetings.Where(x => x.MeetingID == meeting.MeetingID).Select(x => meeting);
}
return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState));
}
}
}