在运行项目并通过Register.cshtml视图发送POST时出现错误。看到以下错误:
InvalidOperationException:无法创建类型为'AdoteRJ.Models.Adotante'的实例。模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数。或者,为'model'参数提供非空的默认值。 Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
在我重构MyEntity类(Adotante)并添加一些复杂类型以使其成为一个丰富的域之后,开始发生此错误。由于某种原因,我尚未发现,尽管成功填写了表单数据(我在开发工具的标头上进行了检查),但此错误已开始并且POST收到错误500。
我正在使用.NET Core 2.1
此外,我在下面发现了与该问题相同的问题,但是它的答案无法解决-如果您添加一个空的构造函数,该错误会消失,但是该表单将被发送为空... Model bound complex types must not be abstract or value types and must have a parameterless constructor
我的代码:
public class Adotante : Entity
{
public Adotante(
Email email,
string senha,
NomeCompleto nomeCompleto,
string foto,
Lar lar
)
{
Email = email;
Senha = senha;
NomeCompleto = nomeCompleto;
Foto = foto;
Lar = lar;
}
public Email Email { get; private set; }
[DataType(DataType.Password)]
[StringLength(100, MinimumLength = 6)]
public string Senha { get; private set; }
public NomeCompleto NomeCompleto { get; private set; }
public string Foto { get; private set; }
public Lar Lar { get; private set; }
#region Adotante Métodos
public void SetEmail(Email email)
{
email = this.Email;
}
#endregion
}
我的ValueObject类之一的示例:
public class Email : ValueObject
{
public Email() { }
public Email(string enderecoEmail)
{
EnderecoEmail = enderecoEmail;
}
[Display(Name = "E-Mail")]
[Required(ErrorMessage = "Por favor, insira o e-mail do usuário/adotante")]
[EmailAddress]
public string EnderecoEmail { get; private set; }
}
}
Register.cshtml类上的表单:
<div class="row">
<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<div class="col-md-1">
<!-- Gap between columns -->
</div>
<div class="col-md-4">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">email</i></span>
<div class="form-line">
<input asp-for="Email.EnderecoEmail" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Senha" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">lock</i></span>
<div class="form-line">
<input asp-for="Senha" class="form-control" />
<span asp-validation-for="Senha" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="NomeCompleto" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">person</i></span>
<div class="form-line">
<input asp-for="NomeCompleto.Nome" class="form-control" />
<span asp-validation-for="NomeCompleto" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Foto" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">insert_photo</i></span>
<div class="form-line">
<input asp-for="Foto" class="form-control" />
<span asp-validation-for="Foto" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.RendaMensal" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">attach_money</i></span>
<div class="form-line">
<input asp-for="Lar.RendaMensal" class="form-control date" />
<span asp-validation-for="Lar.RendaMensal" class="text-danger"></span>
</div>
<span class="input-group-addon">,00</span>
</div>
</div>
<div class="form-group">
<input type="submit" value="Finalizar Cadastro" class="btn btn-primary btn-lg" />
</div>
</div>
<div class="col-md-1">
<!-- Gap between columns -->
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="Lar.PessoasNaCasa" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<input asp-for="Lar.PessoasNaCasa" class="form-control" />
<span asp-validation-for="Lar.PessoasNaCasa" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.MetragemCasa" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<input asp-for="Lar.MetragemCasa" class="form-control" />
<span asp-validation-for="Lar.MetragemCasa" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.TipoLar" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<select asp-for="Lar.TipoLar" asp-items="Html.GetEnumSelectList<TipoLar>()">
<option value="" selected disabled hidden>Selecione</option>
</select>
<span asp-validation-for="Lar.TipoLar" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.CasaComTela" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<select asp-for="Lar.CasaComTela" asp-items="Html.GetEnumSelectList<CasaComTela>()">
<option value="" selected disabled hidden>Selecione</option>
</select>
<span asp-validation-for="Lar.CasaComTela" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.TemCriancas" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<select asp-for="Lar.TemCriancas" asp-items="Html.GetEnumSelectList<TemCriancas>()">
<option value="" selected disabled hidden>Selecione</option>
</select>
<span asp-validation-for="Lar.TemCriancas" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Lar.TemAnimais" class="control-label"></label>
<div class="input-group">
<span class="input-group-addon"><i class="material-icons">home</i></span>
<div class="form-line">
<select asp-for="Lar.TemAnimais" asp-items="Html.GetEnumSelectList<TemAnimais>()">
<option value="" selected disabled hidden>Selecione</option>
</select>
<span asp-validation-for="Lar.TemAnimais" class="text-danger"></span>
</div>
</div>
</div>
</div>
</form>
</div>
答案 0 :(得分:0)
在action方法中为传入的请求模型参数使用[FromBody]
属性可能会有所帮助。像这样:
[HttpPost]
public void Post([FromBody] Adotante incomingRequest)
{
\\posting logic here
}