This project is read-only.

Writing to Database from AdminController

Topics: Writing modules
Sep 25, 2011 at 5:19 AM

I have an Admin page for storing files (not related to any particular content). When I try to use the "Add" method from the controller, I can see my create page, but when I click my submit button, I get a null reference exception:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 146:            public void EndProcessRequest(IAsyncResult result) {
Line 147:                try {
Line 148:                    _httpAsyncHandler.EndProcessRequest(result);
Line 149:                }
Line 150:                finally {

Source File: C:\...\src\Orchard\Mvc\Routes\ShellRoute.cs    Line: 148 

My code looks like:


 public class FilePartRecord : ContentPartRecord
        public virtual string Name { get; set; }
        public virtual string MimeType { get; set; }

    public class FilePart : ContentPart<FilePartRecord>
        public string Name
            get { return Record.Name; }
            set { Record.Name = value; }
        public string MimeType
            get { return Record.MimeType; }
            set { Record.MimeType = value; }

 public class FilePartHandler : ContentHandler
        public FilePartHandler(IRepository<FilePartRecord> repository)


 public class Migrations : DataMigrationImpl {
        public int Create()
            // Creating table FilePartRecord
            SchemaBuilder.CreateTable("FilePartRecord", table => table
                .Column("Name", DbType.String)
                .Column("MimeType", DbType.String)
            return 1;


    public class AdminController : Controller
        private readonly IRepository<FilePartRecord> _repo;
        public AdminController(IRepository<FilePartRecord> repository)
            _repo = repository;

        public ActionResult Index()
            return View(_repo.Table.ToList());


        public ActionResult Add()
            return View(new FilePart());

        public ActionResult Add(FilePart f)
            if (!ModelState.IsValid)
                return View(f);
                _repo.Create(new FilePartRecord { Name = f.Name, MimeType = f.MimeType });
                return RedirectToAction("Index");

Add View
@model FileHider.Models.FilePart
           <h2>Add New File</h2>
@using (Html.BeginFormAntiForgeryPost())
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
@Html.LabelFor(m => m.MimeType)
@Html.EditorFor(m => m.MimeType)
<p><button name="fileSubmit">Add</button></p>

I really haven't found any good documentation on how to setup the AdminController and read/write from the database in a less "Content type" centered approach. Are there any blatant errors in my code?

Sep 27, 2011 at 1:09 AM

Maybe the redirect? Can you try without it, just to test?

Dec 21, 2011 at 12:49 AM

Did you solve this? I am getting the same Exception, in the same line of code, when trying to make my existing MVC application pages load as an Orchard module. My code doesn't have any redirects though. 


Is there anything I need to do in the web.config to make my existing MVC pages work? 


I followed the instructions here: 


Apr 2, 2012 at 9:20 PM

I am also having this issue on the very same line of code.

It has to do with mapping the post values to the model, but I'm not sure how to fix it.

A work around is to change your Create controller method to use FormCollection instead of your specific model object. This works correctly when I tried it.

Apr 2, 2012 at 9:29 PM

FYI solved this a long time ago, but unfortunately I can't remember what I was doing wrong. 

Apr 3, 2012 at 12:12 AM
Edited Apr 3, 2012 at 7:37 AM

I figured out the cause in more detail.

If you're trying to use an object that inherits ContentPart as a POST parameter, Orchard will throw an error because it has it's own routing magic for those ContentParts and ContentTypes that are accessed by strictly following the ContentType/ContentPart patterns.

It will work if you use the underlying ContentRecord as the parameter or any other object that doesn't inherit from ContentPart<SomeType>.