.NET Core Form Submission Throwing NullReferenceException at



提交表单时,我收到以下错误:

异常详细信息

System.NullReferenceException:对象引用未设置为对象的实例。 at Microsoft.Azure.Documents.Document.get_AttachmentsLink(( at Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func'2 getter, Object target( at Microsoft.AspNetCore.Mvc.Internal.DefaultComplexObjectValidationStrategy.Enumerator.MoveNext(( at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy( at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(( at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model( at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d__8.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d__6.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context( at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object&state, Boolean& isComplete( at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( at Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware.d__7.MoveNext(( --- 从引发异常的先前位置的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(( at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext((

我尝试为与模型和Microsoft.Azure.Documents.Document关联的每个属性提供带有隐藏值的表单,但结果相同。此外,我已将 POST 中的参数类型更改为动态,这确实避免了异常。

训练模型(模型(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace Training.Models
{
public class TrainingModel : Microsoft.Azure.Documents.Document
{
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "last_updated")]
public DateTime? LastUpdated { get; set; }
[JsonProperty(PropertyName = "training_sentiment")]
public string TrainingSentiment { get; set; }
[JsonProperty(PropertyName = "sentiment")]
public string Sentiment { get; set; }
[JsonProperty(PropertyName = "score")]
public string SentimentScore { get; set; }
[JsonProperty(PropertyName = "entities")]
public List<Entity> Entities { get; set; }
public class Entity
{
[JsonProperty(PropertyName = "start")]
public long Start { get; set; }
[JsonProperty(PropertyName = "end")]
public long End { get; set; }
[JsonProperty(PropertyName = "value")]
public string Value { get; set; }
[JsonProperty(PropertyName = "orth")]
public string Orth { get; set; }
[JsonProperty(PropertyName = "label")]
public string Label { get; set; }
}
}
}

文档(查看(

<form asp-controller="Tweet" asp-action="Document" method="post" asp-anti-forgery="false">
<!-- Tweet Text -->
<div class="form-group">
<label asp-for="Text" class="control-label">
Document Text
</label>
<div class="well">
@Html.Raw(Model.Text)
</div>
</div>
<!-- Entities -->
<div class="form-group tags">
<label asp-for="Entities">
Name Entity Recognition
</label>
<div class="form-group">
<p>Using your mouse, highlight some text in the document's text field to add entities to the document.</p>
</div>
<div class="form-group">
<div class="col-md-2">
<div>
<label>
Start
</label>
</div>
</div>
<div class="col-md-2">
<div>
<label>
End
</label>
</div>
</div>
<div class="col-md-3">
<div>
<label>
Label
</label>
</div>
</div>
<div class="col-md-4">
<div>
<label>
Value
</label>
</div>
</div>
<div class="col-md-1">
<div></div>
</div>
</div>
@if(Model.Entities != null && Model.Entities.Count > 0) { 
for (int i = 0; i < Model.Entities.Count; i++)
{
<div class="form-group row tag-row">
<div class="col-md-2">
<div>
@Html.TextBoxFor(model => model.Entities[i].Start, null, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<div>
@Html.TextBoxFor(model => model.Entities[i].End, null, new { @class = "form-control" })
</div>
</div>
<div class="col-md-3">
<div>
@Html.TextBoxFor(model => model.Entities[i].Label, null, new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div>
@Html.TextBoxFor(model => model.Entities[i].Value, null, new { @class = "form-control" })
</div>
</div>
<div class="col-md-1">
<div>
<button type="button" class="btn btn-default" data-toggle='tag-row'>
<i class='fa fa-trash-o'></i>
</button>
</div>
</div>
</div>
}
}
</div>
<!-- Sentiment Tag -->
<div class="form-group">
<label asp-for="TrainingSentiment" class="control-label">
Sentiment Tag
</label>
<div>
<div class="btn-toolbar" data-toggle="buttons">
<div class="btn-group">
<label class="btn btn-default @((Model.TrainingSentiment.ToLower() == "positive") ? "active" : string.Empty)">
<input asp-for="TrainingSentiment" type="radio" value="positive" /> <span class="sentimentText">Positive</span>
</label>
<label class="btn btn-default @((Model.TrainingSentiment.ToLower() == "negative") ? "active" : string.Empty)">
<input asp-for="TrainingSentiment" type="radio" value="negative" /> <span class="sentimentText">Negative</span>
</label>
</div>
<div class="btn-group">
<label class="btn btn-default">
<input asp-for="TrainingSentiment" type="radio" value="" /> Reset
</label>
</div>
</div>
</div>
</div>
<div class="ln_solid"></div>
<!-- Buttons -->
<div class="row text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<!-- Hidden Data -->
<input asp-for="Id" type="hidden" />
</form>

推特控制器

public IActionResult Document()
{
var model = ConnectionModel.Tweets.SelectTagged(1)[0];
return View(model);
}
[HttpPost]
public IActionResult Document(TrainingModel model)
{
if (model == null)
return Json(new { error = "null model." });

return Json(model);
}

你不应该从Microsoft.Azure.Documents.Document扩展你的类。此类实质上是来自 Cosmos 服务的动态响应的包装器,并包含实体不需要了解的属性(如附件链接(。从模型中删除此基类,并考虑使用自己的简单DocumentBase类。这样的事情可能会奏效。

public class DocumentBase
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
}

最新更新