Writing to Database from AdminController

Topics: Writing modules
Sep 25, 2011 at 4: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:

Models

 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; }
        }
    }
 
Handler

 public class FilePartHandler : ContentHandler
    {
        public FilePartHandler(IRepository<FilePartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }
    }

Migration

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

AdminController

   [Admin]
    [Themed]
    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());
        }

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

Add View
@model FileHider.Models.FilePart
           <h2>Add New File</h2>
           @Html.ValidationSummary()
@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?

Coordinator
Sep 27, 2011 at 12:09 AM

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

Dec 20, 2011 at 11:49 PM

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: http://stackoverflow.com/questions/5269170/integrate-existing-asp-net-mvc-application-with-orchard-cms/5270458#5270458 

 

Apr 2, 2012 at 8: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 8:29 PM

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

Apr 2, 2012 at 11:12 PM
Edited Apr 3, 2012 at 6: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>.