我正在尝试在我的数据库中保存图像,而我正在使用ASP.NET MVC。 在我添加了图片属性之前,我的表单收集了数据并且一切正常。
这是我的观看代码:
<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>
因此它是一个包含许多属性的简单HTML表单。我已添加上传按钮以上传图像,并开始在控制器中更改创建方法的代码:
public IActionResult Create()
{
var memberRefViewModel = new CreateMemberViewModel();
return View(memberRefViewModel);
}
[HttpPost]
public IActionResult Create(CreateMemberViewModel createMemberViewModel, HttpPostedFileBase image)
{
if (image != null)
{
createMemberViewModel.Image = new byte[image.ContentLength];
image.InputStream.Read(createMemberViewModel.Image, 0, image.ContentLength);
}
if (ModelState.IsValid)
{
//TODO ADD TRY CATCH
MemberEntity mem = Mapper.Map<MemberEntity>(createMemberViewModel);
var tenantId = sessionHelper.GetTenantIdFromSession().Value;
TenantEntity t = this.tenantService.Get(tenantId);
mem.Tenant = t;
mem.IsActive = true;
this.service.Add(mem);
this.gcmService.SendGCMNotification("New member has been created.");
return RedirectToAction("Index").WithSuccess($"{createMemberViewModel.Title} {createMemberViewModel.FirstName} {createMemberViewModel.LastName} created successfully.");
}
else
{
return View(createMemberViewModel).WithError("Create not valid. Please check fields are valid.");
}
}
我已经添加了HttpPostedFileBase,我正在尝试检查图像是否为空。我这样做的那一刻,我开始异常说:
InvalidOperationException: Instances of abstract classes cannot be created.
我完全明白了,但我不明白是什么原因引起了这个问题?
我的第一个想法是,这与我使用的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()
这是我的模特:
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));
}
}
我很确定在旧的MVC中这可以工作,但是如何让它在ASP.NET Core中运行?有人知道这有什么问题吗?