2011-07-18 67 views
0

我有嚴重的問題當表單提交時,我無法將我的數據傳遞給控制器​​如何解決此問題?我如何綁定我的數據源來查看?

//int controller class 
[HttpPost] 
public ActionResult Index(EnterpriseFramework.Entity.Synchronization.BindableEntity model) 
{ 
    //do something 
} 

和我的觀點:

@model EnterpriseFramework.Entity.Synchronization.BindableEntity 

<p> 
    @using (Html.BeginForm()) 
    { 
     <fieldset> 
      <legend>title</legend> 
      <div> 
       @Html.HiddenFor(m => (m.Underlay.Entity as AutomationTest.Models.DTO.Letter).oau_Letter_Id) 
      </div> 
      <div>     
       @Html.LabelFor(m => (m.Underlay.Entity as AutomationTest.Models.DTO.Letter).oau_Letter_Number) 
       @Html.TextBoxFor(m => (m.Underlay.Entity as AutomationTest.Models.DTO.Letter).oau_Letter_Number) 
      </div> 
      <div> 
       @{ 
        EnterpriseFramework.Entity.Synchronization.DataSource ds = Model.GetRelation("lnkLetterReceiver"); 
        foreach (EnterpriseFramework.Entity.Synchronization.BindableEntity item in ds) 
        { 
         AutomationTest.Models.DTO.LetterReceiver childRece = item.Underlay.Entity as 
          AutomationTest.Models.DTO.LetterReceiver; 
         <div>        
          @Html.LabelFor(c=> childRece.oau_LetterReceiver_Name) 
          @Html.TextBoxFor(c=> childRece.oau_LetterReceiver_Name) 
         </div> 
        } 
       }    
      </div> 
      <div> 
       <input type="submit" name="Confirm" value="Confirm" /> 
      </div> 
     </fieldset> 
    } 
</p> 

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.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +199 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +572 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 
    System.Web.Mvc.Controller.ExecuteCore() +116 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50 
    System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8897857 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 
+0

我有一些父母和孩子的關係 – kamiar3001

回答

1

每種類型,您正試圖爲動作參數使用必須有一個默認參數的構造函數。否則,默認模型聯編程序將不能實例化並填充其屬性。

這就是爲什麼你不應該在視圖中使用你的域模型。您應該定義和使用專門設計的類視圖模型以滿足給定視圖的要求。然後,控制器動作將在視圖模型和域模型之間來回映射。像這樣:

[HttpPost] 
public ActionResult Index(MyViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // There were validation errors => redisplay the view 
     return View(model); 
    } 

    // the model is valid => map the view model to a domain model and process 
    ... 
} 

就最佳實踐而言。如果您的應用程序已被污染,對視圖模型重構是不可能的時刻,你有兩種可能性:

  1. 寫出BindableEntity類型的定製模型綁定,讓你手動調用適當的構造函數中CreateModel方法。
  2. BindableEntity類型添加默認無參數構造函數。
  3. 使用TryUpdateModel方法:

    [HttpPost] 
    public ActionResult Index() 
    { 
        var model = new BindableEntity(WHATEVER); 
        if (!TryUpdateModel(model) || !ModelState.IsValid) 
        { 
         // There were validation errors => redisplay the view 
         return View(model); 
        } 
    
        // the model is valid => process 
        ... 
    } 
    
+0

我得到的數據,我需要保存用戶的變化。 – kamiar3001

+0

它給我的資源無法顯示,當我添加[httppost]索引()無參數 – kamiar3001

+0

[HttpPost] public ActionResult Index(){} //它不起作用 – kamiar3001

0

我假設你只有帶參數定義構造函數?爲了解決這個問題,你需要添加這行代碼

public BindableEntity() 
{ } 

要將EnterpriseFramework.Entity.Synchronization.BindableEntity類。

這將定義一個無參數的構造函數,並允許您按需要使用它,但您需要定義ViewModel以便按照設計的方式使用MVC。

相關問題