views:

255

answers:

1

I am a newbie to ASP.NET MVC (v2), and I am trying to use a strongly-typed view tied to a model object that contains two optional multi-select listbox objects. Upon clicking the submit button, these objects may have 0 or more values selected for them.

My model class looks like this:

using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace ModelClasses.Messages
{
    public class ComposeMessage
    {
        public bool is_html { get; set; }
        public bool is_urgent { get; set; }
        public string message_subject { get; set; }
        public string message_text { get; set; }
        public string action { get; set; }
        public MultiSelectList recipients { get; set; }
        public MultiSelectList recipient_roles { get; set; }

        public ComposeMessage()
        {
            this.is_html = false;
            this.is_urgent = false;
            this.recipients = new MultiSelectList(new Dictionary<int, string>(), "Key", "Value");
            this.recipient_roles = new MultiSelectList(new Dictionary<int, string>(), "Key", "Value");
        }
    }
}

My view looks like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ModelClasses.Messages.ComposeMessage>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Compose A Message
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
 Compose A New Message:</h2>
 <br />
<span id="navigation_top">
<%= Html.ActionLink("\\Home", "Index", "Home") %><%= Html.ActionLink("\\Messages", "Home") %></span>
<% using (Html.BeginForm())
 { %>
<fieldset>
<legend>Message Headers</legend>
<label for="message_subject">
Subject:</label>
<%= Html.TextBox("message_subject")%>
<%= Html.ValidationMessage("message_subject")%>
<label for="selected_recipients">
Recipient Users:</label>
<%= Html.ListBox("recipients") %>
<%= Html.ValidationMessage("selected_recipients")%>
<label for="selected_recipient_roles">
Recipient Roles:</label>
<%= Html.ListBox("recipient_roles") %>
<%= Html.ValidationMessage("selected_recipient_roles")%>
<label for="is_urgent">
Urgent?</label>
<%= Html.CheckBox("is_urgent") %>
<%= Html.ValidationMessage("is_urgent")%>
</fieldset>
<fieldset>
<legend>Message Text</legend>
<%= Html.TextArea("message_text") %>
<%= Html.ValidationMessage("message_text")%>
</fieldset>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="action" id="send_message" value="Send" />
<% } %>
<span id="navigation_bottom">
<%= Html.ActionLink("\\Home", "Index", "Home") %><%= Html.ActionLink("\\Messages", "Home") %></span>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Scripts" runat="server">
</asp:Content>

I have a parameterless ActionResult in my MessagesController like this:

    [Authorize]
    public ActionResult ComposeMessage()
    {
        ModelClasses.Messages.ComposeMessage FormData = new ModelClasses.Messages.ComposeMessage();
        Common C = (Common)Session["Common"];
        FormData.recipients = new MultiSelectList(C.AvailableUsers, "Key", "Value");
        FormData.recipient_roles = new MultiSelectList(C.AvailableRoles, "Key", "Value");
        return View(FormData);
    }

...and my model-based controller looks like this:

    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ComposeMessage(ModelClasses.Messages.ComposeMessage FormData)
    {
        MyUserClass CurrentUser = (MyUserClass )Session["CurrentUser"];            
        Common C = (Common)Session["Common"];
    //... (business logic)
    return View(FormData);
    }

Problem is, I can access the page fine before a submit. When I actually make selections and press the submit button, however, I get:

Server Error in '/' Application. No parameterless constructor defined for this object. 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.MissingMethodException: No parameterless constructor defined for this object. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86 System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230 System.Activator.CreateInstance(Type type, Boolean nonPublic) +67 System.Activator.CreateInstance(Type type) +6 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +307 System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult) +495 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +473 System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +45 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +642 System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +144 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +95 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2386 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +539 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +447 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +173 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +801 System.Web.Mvc.Controller.ExecuteCore() +151 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +105 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +36 System.Web.Mvc.<>c_DisplayClass8.b_4() +65 System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +44 System.Web.Mvc.Async.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +42 System.Web.Mvc.Async.WrappedAsyncResult1.End() +140 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +36 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8677678 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082

This error shows up before I can trap it. I have no idea where it's choking, or what it's choking on. I don't see any point of this model that cannot be created with a parameterless constructor, and I can't find out where it's dying... Help is appreciated, thanks.

-Jeremy

A: 

Strongly typing recipients and recipient_roles as MultiSelectList may be causing this problem. Try the following simple mod to your code:

public object recipients { get; set; } 
public object recipient_roles { get; set; 
Martin Webster