TypeAccessException in ASP.NET MVC 5

We just updated our application to ASP.NET MVC 5 and now we get an exception System.TypeAccessException in Data action when I call

return new SchedulerAjaxData(events);

It seems to be the same issue as described in this thread related to the new security model in MVC 5:

forums.asp.net/t/1939805.aspx


Stack:

An exception of type ‘System.TypeAccessException’ occurred in Application.dll but was not handled in user code

Additional information: Attempt by security transparent method ‘DHTMLX.Scheduler.Data.SchedulerAjaxData.op_Implicit(DHTMLX.Scheduler.Data.SchedulerAjaxData)’ to access security critical type ‘System.Web.Mvc.ContentResult’ failed.

Assembly ‘DHTMLX, Version=3.0.5045.24291, Culture=neutral, PublicKeyToken=53f24791214eb8ac’ is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.

Hello,
thank you for the reporting, we are investigating the issue. As a temporary workaround, you may try several things
1)user the default constructor of DHXScheduler object and specify Save/Data urls manually

var scheduler = new DHXScheduler(); scheduler.DataAction = Url.Action("Data"); scheduler.SaveAction = Url.Action("Save");
2) and do not use implicit type conversion for responses of Data and Save actions, and return Action’s responce via Controller.Content method. I.e.

Save:
instead of

return (ContentResult)new AjaxSaveResponse(action);

use following: return Content(new AjaxSaveResponse(action), "text/xml"); And the same with Data action:

return Content( new SchedulerAjaxData(...), "text/json");//note different content types

Thank you Aliaksandr!!

I changed those two lines of code and now it works perfectly :slight_smile:

Hi,

I had the same problem, Thanks for the tip.
You should rebuild your dll with the last Framework 4.5 … :slight_smile:

Thanks again.

PAF

Sadly, this issue is active again with MVC 5.1.1, any news on when a compatible nuget will be available?

I had a similar issue as the original poster and implemented the suggested fixes but now events don’t show when created and aren’t saving to the DB, any thoughts? Here is my controller code:

[code]namespace WebApplication10.Controllers
{
public class SchedulerController : Controller
{
private EventContext db = new EventContext();

    public ActionResult Index()
    {
        var sched = new DHXScheduler();
        sched.DataAction = Url.Action("Data");
        sched.SaveAction = Url.Action("Save");
        //Url.Action("Save", "ControllerName");
        sched.Skin = DHXScheduler.Skins.Terrace;
        sched.LoadData = true;
        sched.EnableDataprocessor = true;
        sched.InitialDate = new DateTime(2013, 5, 5);
        return View(sched);
    }

    public ContentResult Data()
    {
        return Content(new SchedulerAjaxData(db.Events.Select(e => new { e.EventId, e.Title, e.Start, e.End })), "text/json");
        //return (new SchedulerAjaxData(db.Events.Select(e => new { e.EventId, e.Title, e.Start, e.End })));
    }



    public ContentResult Save(int? id, FormCollection actionValues)
    {
        var action = new DataAction(actionValues);
        var changedEvent = DHXEventsHelper.Bind<Event>(actionValues);
       
        try
        {
            switch (action.Type)
            {
                case DataActionTypes.Insert:
                    db.Events.Add(changedEvent);
                    break;
                case DataActionTypes.Delete:
                    changedEvent = db.Events.FirstOrDefault(ev => ev.EventId == action.SourceId);
                    db.Events.Remove(changedEvent);
                    break;
                default:// "update"   
                    var target = db.Events.Single(e => e.EventId == changedEvent.EventId);
                    DHXEventsHelper.Update(target, changedEvent, new List<string> { "EventId" });
                    break;
            }
            db.SaveChanges();
            action.TargetId = changedEvent.EventId;
        }
        catch (Exception a)
        {
            action.Type = DataActionTypes.Error;
        }
        return Content(new AjaxSaveResponse(action), "text/xml");
        //return (new AjaxSaveResponse(action));
    }


}

}[/code]

Hi sesellis,
can you check browser console, are there any JS errors on the page? Also you may set a breakpoint inside the Data and Save methods, to make sure they are called from the client

I ended up sorting it out, it was looking for CalendarEvent objects and I was feeding it Event objects. Once I renamed my event objects it started saving/updating just fine. However, I would like to know how it can be configured to use a different class name? CalendarEvent appeared to be the default somehow and I’d like to use an existing class.

Thanks,
Sam

Hello,

I was getting the typeAccessException and I read your forum and did the changes as described in “save” and “data”. However, now it does not save the event since it does not get the correct data from the view and it gives time overflow error in save method. Here is my code:

public class CalendarController : Controller
{

    public ActionResult Index()
    {
        //Being initialized in that way, scheduler will use CalendarController.Data as a the datasource and CalendarController.Save to process changes
       // var scheduler = new DHXScheduler();

        

        /*
         * It's possible to use different actions of the current controller
         *      var scheduler = new DHXScheduler(this);     
         *      scheduler.DataAction = "ActionName1";
         *      scheduler.SaveAction = "ActionName2";
         * 
         * Or to specify full paths
         *      var scheduler = new DHXScheduler();
         *      scheduler.DataAction = Url.Action("Data", "Calendar");
         *      scheduler.SaveAction = Url.Action("Save", "Calendar");
         */

        /*
         * The default codebase folder is ~/Scripts/dhtmlxScheduler. It can be overriden:
         *      scheduler.Codebase = Url.Content("~/customCodebaseFolder");
         */
        var scheduler = new DHXScheduler();
        scheduler.DataAction = Url.Action("Data", "Calendar");
        scheduler.SaveAction = Url.Action("Save", "Calendar");

        scheduler.LoadData = true;
        scheduler.EnableDataprocessor = true;

        return View(scheduler);
    }

    public ContentResult Data()
    {
        return Content(new SchedulerAjaxData(new ProjectDataDataContext().Events), "text/json");

    }

    public ContentResult Save(int? id, FormCollection actionValues)
    {
        var action = new DataAction(actionValues);
        
        try
        {
            var changedEvent = (Event)DHXEventsHelper.Bind(typeof(Event), actionValues);
            var data = new ProjectDataDataContext();
 

            switch (action.Type)
            {
                case DataActionTypes.Insert:
                    //do insert
                    data.Events.InsertOnSubmit(changedEvent);
                 
                    break;
                case DataActionTypes.Delete:
                    //do delete
                    changedEvent = data.Events.SingleOrDefault(ev => ev.Id == action.SourceId);
                    data.Events.DeleteOnSubmit(changedEvent);
                    break;
                default:// "update"                          
                    var eventToUpdate = data.Events.SingleOrDefault(ev => ev.Id == action.SourceId);
                    DHXEventsHelper.Update(eventToUpdate, changedEvent, new List<string>() { "id" });
                    break;
            }
            data.SubmitChanges();
            action.TargetId = changedEvent.Id;
        }
        catch
        {
            action.Type = DataActionTypes.Error;
        }
        //return (ContentResult)new AjaxSaveResponse(action);
        return Content(new AjaxSaveResponse(action), "text/xml");
    }
}

}

can someone please help me it is really urgent!!!