문제

저는 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

이 오류는 내가 그것을 가리기 전에 나타납니다. 나는 그것이 어디에서 질식하는지 또는 무엇을 질식하고 있는지 전혀 모른다. 매개 변수가없는 생성자로 만들 수없는이 모델의 어떤 점도 보이지 않으며 어디에서 죽어 가는지 알 수 없습니다 ... 도움을 주셔서 감사합니다. 감사합니다.

-예레미

도움이 되었습니까?

해결책

수신자와 수신자 _roles를 다중 선거인으로 강력하게 입력하면이 문제가 발생할 수 있습니다. 코드에 대한 다음 간단한 모드를 사용해보십시오.

public object recipients { get; set; } 
public object recipient_roles { get; set; 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top