2010-05-08 70 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) %> 

得好好的,現在時間對我來說,找出原因。

相關問題