简体   繁体   English

将数据传递到Asp.Net Core绑定模型中

[英]Passing data into an Asp.Net Core bound model

I have an Asp.Net Core 2 project, whereby I'm trying to change the data in a class from a form; 我有一个Asp.Net Core 2项目,正在尝试从表单更改类中的数据。 here's my form: 这是我的表格:

@model MyProject.Model
@{
    ViewData["Title"] = " ...";
}
. . . 
    <form asp-action="MyMethod" asp-controller="MyController">
        <input type="text" asp-for="MyClass.MyValue" />

    </form>

The referenced model looks like this: 引用的模型如下所示:

public class Model
{
    public MyClass MyClass { get; set; }

    public Model(MyClass myClass)
    {
        MyClass = myClass
    }

The idea being that MyClass gets registered with the IoC initially, and its current value gets passed into the web page. 这个想法是MyClass最初在IoC中注册,并且其当前值被传递到网页中。 The user can then change the values and post it on to MyController . 然后,用户可以更改值并将其发布到MyController The controller looks like this: 控制器如下所示:

public class MyController : Controller
{
    [HttpPost]
    public async Task<IActionResult> MyMethod(Model myModel)
    {

However, when I execute this and call MyMethod , I get the error: 但是,当我执行此操作并调用MyMethod ,出现错误:

InvalidOperationException: Could not create an instance of type 'Model'. InvalidOperationException:无法创建“模型”类型的实例。 Model bound complex types must not be abstract or value types and must have a parameterless constructor. 模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数。 Alternatively, give the 'myModel' parameter a non-null default value. 或者,为“ myModel”参数提供一个非空的默认值。

Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext) Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)

So, it appears from the error that what I'm trying to do is explicitly prohibited by Asp.Net Core. 因此,从错误看来,Asp.Net Core明确禁止了我要执行的操作。 I'm curious as to why that is the case (since it has the object in the IoC), but this is presumably well trodden ground, so how can I pass this class in? 我很好奇为什么会这样(因为它在IoC中有对象),但这大概是一个被践踏的领域,那么我该如何传递此类?

EDIT: 编辑:

The class is registered in Startup.cs using the Asp.Net core IoC container: 该类使用Asp.Net核心IoC容器在Startup.cs中注册:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTransient<MyClass, MyClass>();

You Need to update model as below. 您需要如下更新模型。 You need to create default constructor as when we post complex model because during model binding engine can't identify with parameter constructor. 您需要在发布复杂模型时创建默认构造函数,因为在模型绑定引擎期间无法使用参数构造函数进行标识。

public class Model
{
    public MyClass MyClass { get; set; }
    public Model()
    {
        //MyClass = myClass;
    }
    public Model(MyClass myClass)
    {
        MyClass = myClass;
    }
}

My action method 我的动作方法

[HttpPost]
public async Task<IActionResult> MyMethod(Model myModel)
{
   //return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM