C# InvalidOperationException:尝试将映像传递给MVC中的控制器时,无法创建抽象类的实例
我正在尝试在我的数据库中保存图像,我正在使用ASP.NET MVC。 在添加图像属性之前,我有一个收集数据的表单,所有的表单都正常工作 这是我的视图代码:C# InvalidOperationException:尝试将映像传递给MVC中的控制器时,无法创建抽象类的实例,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我正在尝试在我的数据库中保存图像,我正在使用ASP.NET MVC。 在添加图像属性之前,我有一个收集数据的表单,所有的表单都正常工作 这是我的视图代码: <div id="createMemberModel" class="modal"> <div class="modal-content" style="width:40%"> <form asp-antiforgery="true" asp-action="Create" method=
<div id="createMemberModel" class="modal">
<div class="modal-content" style="width:40%">
<form asp-antiforgery="true" asp-action="Create" method="post">
<div class="panel panel-default">
<div class="panel-heading">
<span class="panel-title">
Create new member
</span>
<span class="close glyphicon glyphicon-remove-sign"></span>
</div>
<div class="panel-body">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-horizontal">
<div class="panel-group">
<labe class="control-label col-md-4">Title:</labe>
<div class="col-md-8">
<select name="Title" class="form-control" required>
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Mx</option>
<option>Miss</option>
<optgroup label="Legislative and executive">
<option>Representative</option>
<option>Senator</option>
<option>Speaker</option>
<option>President</option>
<option>Councillor</option>
<option>Delegate</option>
<option>Mayor</option>
<option>Governor </option>
<option>Secretary</option>
</optgroup>
</select>
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">First Name:</label>
<div class="col-md-8">
<input name="FirstName" class="form-control" required />
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">Last Name:</label>
<div class="col-md-8">
<input name="LastName" class="form-control" required />
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">Phone Number:</label>
<div class="col-md-8">
<input name="Phone" class="form-control" required />
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">Party:</label>
<div class="col-md-8">
<input name="Party" class="form-control" required />
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">Email Address:</label>
<div class="col-md-8">
<input name="Email" class="form-control" required />
</div>
</div>
</div>
<br /><br />
<div class="form-horizontal">
<div class="panel-group">
<label class="control-label col-md-4">Browse: </label>
<div class="col-md-8">
<input class="form-control" id="image" name="image" type="file" hidden>
</div>
</div>
</div>
<br /><br /><br />
<div class="form-horizontal">
<div class="panel-group pull-right">
<div class="col-md-1">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
我完全明白,但我不明白是什么导致了这个问题
我的第一个想法是,这与我使用的Automapper有关。我在想,绑定可能不好,或者类似的东西,但在模型中添加绑定后,我仍然有相同的绑定
问题发生在我单击“创建”按钮的同一秒,它从未触及后端代码制动点。所以我想我在这个观点上遗漏了什么
如果这意味着什么,这些是原始异常详细信息:
System.InvalidOperationException: Instances of abstract classes cannot be created.
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.<BindModelCoreAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindModelAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindArgumentsCoreAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAllActionFiltersAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeExceptionFilterAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__6.MoveNext()
System.InvalidOperationException:无法创建抽象类的实例。
在System.Runtime.CompilerServices.RuntimeHelpers.\u CompileMethod(IRuntimeMethodInfo方法)
位于System.Reflection.Emit.DynamicMethod.CreateDelegate(类型delegateType,对象目标)
在System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()中
位于System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda,DebugInfoGenerator DebugInfoGenerator)
位于Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext绑定上下文)
在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d_u4.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d_u8.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d_u6.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u26.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u25.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u18.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Builder.RouterMiddleware.d_u4.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.d_u18.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.d_u18.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.d_u18.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.d_u18.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.d_u4.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.d_u7.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d_u6.MoveNext()上
这是我的模型:
public class CreateMemberViewModel : IMapFrom<MemberEntity>, IMapTo<MemberEntity>, IHaveCustomMappings
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsActive { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Party { get; set; }
public byte[] Image { get; set; }
public string FullName { get { return $"{Title} {FirstName} {LastName}"; } }
public void CreateMappings(IMapperConfigurationExpression configuration)
{
configuration.CreateMap<CreateMemberViewModel, MemberEntity>()
.ForMember(t => t.Image, opt => opt.ResolveUsing(t => t.Image == null ? null : t.Image));
}
}
public类CreateMemberViewModel:IMapFrom、IMapTo、IHaveCustomMappings
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共bool IsActive{get;set;}
公共字符串电子邮件{get;set;}
公用字符串电话{get;set;}
公共字符串参与方{get;set;}
公共字节[]映像{get;set;}
公共字符串全名{get{return$“{Title}{FirstName}{LastName}”;}
公共无效信用证
System.InvalidOperationException: Instances of abstract classes cannot be created.
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.<BindModelCoreAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindModelAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindArgumentsCoreAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAllActionFiltersAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeExceptionFilterAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__6.MoveNext()
public class CreateMemberViewModel : IMapFrom<MemberEntity>, IMapTo<MemberEntity>, IHaveCustomMappings
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsActive { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Party { get; set; }
public byte[] Image { get; set; }
public string FullName { get { return $"{Title} {FirstName} {LastName}"; } }
public void CreateMappings(IMapperConfigurationExpression configuration)
{
configuration.CreateMap<CreateMemberViewModel, MemberEntity>()
.ForMember(t => t.Image, opt => opt.ResolveUsing(t => t.Image == null ? null : t.Image));
}
}