2010-05-08 69 views
0

我使用asp.net mvc 2.0(默认绑定模型),我有这个问题。如何将dropdownlist数据绑定到复杂类?

我有了一个下拉列表

<%= Html.DropDownList("List", "-----")%> 

现在我有一个强类型的视图模型类像

Public class Test 
{ 
    public List { get; set; } 
    public string Selected {get; set;} 

    public Test() 
    { 
      List = new List(); 
      selected = ""; 
    } 
} 

现在,我有我的控制器现在这个

public ActionResult TestAction() 
    { 
     Test ViewModel = new Test(); 
     ViewModel.List = new SelectList(GetList(), "value", "text", "selected"); 
     return View(Test); 
    } 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult TestAction(Test ViewModel) 
    { 
      return View(); 
    } 

当我第一次加载TestAction页面时,它会按预期填充下拉列表。

现在我想将选定的值发回服务器(下拉列表位于带有其他文本框的表格标签内)。所以我试图自动绑定它,当它看到(测试ViewModel)

但是,我得到这个大讨厌的错误。

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, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 
    System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98 
    System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 
    System.Activator.CreateInstance(Type type, Boolean nonPublic) +69 
    System.Activator.CreateInstance(Type type) +6 
    System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +403 
    System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult) +544 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +479 
    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) +658 
    System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +147 
    System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +98 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2504 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +548 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +474 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +181 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830 
    System.Web.Mvc.Controller.ExecuteCore() +136 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111 
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 
    System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141 
    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) +38 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8836913 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

那么我该怎么做?

回答

1

你有没有考虑使用强类型的辅助努力呢?

我在做类似的事情我有多个下拉菜单的类别选择的看法和我使用下面的代码:

<%= Html.DropDownListFor(model => model.Selected, Model.List) %> 

你可以试试..

为什么你有括号后公共类测试(这是不正确的语法,据我所知)?

Public class Test() <- 
{ 
    public List { get; set; } 
    public string Selected {get; set;} 
} 
+0

对不起,我在深夜写这篇文章。那不应该在那里。第一次发布的页面不是真正的问题,它是后期发布的问题。 – chobo2 2010-05-08 17:22:19

+0

嗯我试过你的linq方式,它似乎工作。我不明白,但它的工作原理!我也觉得奇怪你如何做linq有点不同于表达式与源代码分开。 – chobo2 2010-05-08 18:09:43

+0

使用'Model.List'获取List的值,并通过'model => model.Selected'将其应用于模型的Selected属性。 – 2010-05-09 15:32:27

0

看起来像反射代码期望您的模型对象上的无参数构造函数。你的Test类没有一个。

这应该更好的工作:

Public class Test() 
{ 
    public Test() 
    { 
     this.List = new List(); 
     this.Selected = string.Empty; 
    } 

    public List { get; set; } 
    public string Selected {get; set;} 
} 
+0

应该有一个应该生成的自动默认值。无论如何,我也尝试,以及它仍然无法正常工作。 – chobo2 2010-05-08 17:13:06

0

谢谢你Shaharyar !! (和俄德......你的“更好的解决方案”给出了同样的错误,因为他最初发布有关)

<%= Html.DropDownListFor(model => model.Selected, Model.List) %> 

得好好的,现在时间对我来说,找出原因。

相关问题