0

我是 ASP.NET MVC (v2) 的新手,我正在尝试使用与包含两个可选多选列表框对象的模型对象绑定的强类型视图。单击提交按钮后,这些对象可能会为它们选择 0 个或多个值。

我的模型类如下所示:

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");
        }
    }
}

我的观点是这样的:

<%@ 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>

我的 MessagesController 中有一个无参数的 ActionResult,如下所示:

    [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);
    }

...我的基于模型的控制器如下所示:

    [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);
    }

问题是,我可以在提交之前访问该页面。但是,当我实际进行选择并按下提交按钮时,我得到:

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

这个错误在我捕获它之前就出现了。我不知道它在哪里窒息,或者它在窒息什么。我没有看到无法使用无参数构造函数创建此模型的任何点,而且我无法找出它正在死去的地方......感谢您的帮助,谢谢。

-杰里米

4

1 回答 1

1

将收件人和收件人角色强输入为 MultiSelectList 可能会导致此问题。对您的代码尝试以下简单的 mod:

public object recipients { get; set; } 
public object recipient_roles { get; set; 
于 2010-05-16T07:04:14.377 回答