How can I bind my data source for viewing?

I have a serious problem. I can not transfer my data to the controller when the form is submitted, how can I solve this?

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

and my opinion:

@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
source share
2 answers

Each type that you are trying to use as an action parameter should have a constructor without parameters without parameters. Otherwise, the default mediator will not be able to instantiate and populate its properties.

. , , . . :

[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 
    ...
}

. , :

  • BindableEntity, CreateModel.
  • BindableEntity.
  • 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 
        ...
    }
    
+1

, ? ,

public BindableEntity()
{ }

EnterpriseFramework.Entity.Synchronization.BindableEntity.

, ViewModel, MVC , .

0

Source: https://habr.com/ru/post/1541217/


All Articles