【发布时间】:2017-12-30 05:35:09
【问题描述】:
提交表单时,我收到以下错误:
异常详情
System.NullReferenceException:对象引用未设置为对象的实例。 在 Microsoft.Azure.Documents.Document.get_AttachmentsLink() 在 Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func`2 getter,对象目标) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultComplexObjectValidationStrategy.Enumerator.MoveNext() 在 Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy 策略) 在 Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType() 在 Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata 元数据,字符串键,对象模型) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d__8.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d__6.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext 上下文) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态&下一个,范围&范围,对象&状态,布尔& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware.d__7.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()
我尝试为与模型和 Microsoft.Azure.Documents.Document 关联的每个属性提供隐藏值的表单,但结果相同。另外,我将 POST 中的参数类型更改为动态,确实避免了异常。
TrainingModel(模型)
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>
TweetController
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);
}
【问题讨论】:
标签: c# asp.net-mvc azure-cosmosdb