如何解决一个ViewComponent中的SQLNullValueException异常
我可能在这里遗漏了一些明显的东西...
我在同一页面上有两个用于查找表的ViewComponent。除了从不同的控制器中拉取之外,ViewComponents相同:
public class FormRestrictionViewComponent : ViewComponent
{
private readonly IFormRepository formRepository;
public FormRestrictionViewComponent(IFormRepository formRepository)
{
this.formRepository = formRepository;
}
public IViewComponentResult Invoke()
{
var result = formRepository.GetAllForms();
return View(result);
}
}
public class DNSReasonViewComponent : ViewComponent
{
private readonly IDNSReasonRepository dNSReasonRepository;
public DNSReasonViewComponent(IDNSReasonRepository dNSReasonRepository)
{
this.dNSReasonRepository = dNSReasonRepository;
}
public IViewComponentResult Invoke()
{
var result = dNSReasonRepository.GetAllDNSReasons();
return View(result);
}
}
类似地,每个调用都相当相同:
@model IEnumerable<Polyhymnia.Models.Form>
@{
var formSelectListItems = Model.Select(Form => new SelectListItem
{ Text = Form.FormName,Value = Form.FormID.ToString() });
}
@Html.DropDownList("Free Verse",formSelectListItems)
@model IEnumerable<Polyhymnia.Models.DNSReason>
@{
var selectListItems = Model.Select(DNSReason => new SelectListItem
{ Text = DNSReason.DNSReasonDescription,Value = DNSReason.DNSReasonID.ToString() });
}
@Html.DropDownList("Title",selectListItems)
[我尝试使用“标题”(Title)进行FormRestriction,更改了它以确保没有冲突。如果我正确阅读了工具提示,则“ Free Verse”将是下拉菜单中的默认选项。]
...以及页面本身上...
<div class="form-group row">
<label asp-for="Market.DNSReasonId" class="col-sm-3 col-form-label"></label>
<div id="partial">
@await Component.InvokeAsync("DNSReason")
</div>
<button type="button" class="btn btn-info" id="DNSAddBtn" asp-controller="Positions" asp-action="Create"
data-toggle="ajax-modal" data-target="#DNSAddModal">
Add DNS Reason
</button>
<br />
...
<hr />
<div class="form-group-row">
<center><h3>Form Restrictions</h3></center>
</div>
<table id="FormRestrictionsTable" width="500px" border="1">
<tr>
<td><input type="checkbox" name="chk" /></td>
<td> <div id="formrestrictionpartial"> @await Component.InvokeAsync("FormRestriction") </div> </td>
<td> <button type="button" class="btn btn-info" id="FRAddBtn" asp-controller="Positions" asp-action="Create"
data-toggle="ajax-modal" data-target="#FRAddModal">Add New Form</button> </td>
</tr>
</table>
[注意:我尝试将FormRestriction调用从表中拉出,但出现了相同的错误。]
[而且,当ViewComponent类仅使用Invoke()时,您可以使用InovkeAsync(),但它在DNSReason上有效,因此,我对此感到怀疑。]
DNSReason工作正常。 FormRestriction给我错误“ SqlNullValueException:数据为Null。不能在Null值上调用此方法或属性。”跟踪的前三分之一:
Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
Microsoft.Data.SqlClient.SqlDataReader.GetInt32(int i)
lambda_method(Closure,QueryContext,DbDataReader,ResultContext,int[],ResultCoordinator )
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<T>+Enumerator.MoveNext()
System.Linq.Enumerable+SelectEnumerableIterator<TSource,TResult>.ToList()
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateGroupsAndOptions(string optionLabel,IEnumerable<SelectListItem> selectList,ICollection<string> currentValues)
Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateSelect(ViewContext viewContext,ModelExplorer modelExplorer,string optionLabel,string expression,ICollection<string> currentValues,bool allowMultiple,object htmlAttributes)
Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateSelect(ViewContext viewContext,object htmlAttributes)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GenerateDropDown(ModelExplorer modelExplorer,object htmlAttributes)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.DropDownList(string expression,object htmlAttributes)
Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperSelectExtensions.DropDownList(IHtmlHelper htmlHelper,IEnumerable<SelectListItem> selectList)
Polyhymnia.Pages.Shared.Components.FormRestriction.Pages_Shared_Components_FormRestriction_Default.ExecuteAsync() in Default.cshtml
-
@model IEnumerable<Polyhymnia.Models.Form>
@{
var formSelectListItems = Model.Select(Form => new SelectListItem
{ Text = Form.FormName,Value = Form.FormID.ToString() });
}
@Html.DropDownList("Free Verse",formSelectListItems)
两个模型都包含在“模型”文件夹中。它们并不完全相同(DNSReason仅具有ID和描述,Form具有ID,名称和描述),但足以胜任政府工作。控制器在所有意图和目的上都是相同的。
这应该是唯一相关的部分,并且在DNSReason中相同,只是更改了模型名称:
public class SQLFormRepository : IFormRepository
{
private readonly AppDbContext context;
public SQLFormRepository(AppDbContext context)
{
this.context = context;
}
public IEnumerable<Form> GetAllForms()
{
return context.Form;
}
}
我还通过在Form查找表中抛出一些记录来确保调用没有返回空集。错误没有变化。
也(这可能是问题所在,而我对此还不足以找出区别):调用这两个表(市场)的表直接引用了DNSReasonID。另一方面,窗体具有一个称为FormRestriction的中间表,该表充当停止多对多关系的中介(一个市场可能只有一个DNS原因,但它可能具有任意数量的窗体限制)。因此,Market包含一个FormRestrictionID列,但不包含一个FormID列(FormRestriction包含FormRestrictionID,FormID和MarketID,仅此而已)。但是,我不认为这会导致这种情况发生。
我只是第1000次浏览了代码,而在我键入“ FormRestriction”的地方仍然看不到任何错字,所以我很茫然。
在尝试发布尽可能少的代码时,我可能会忘记一些必要的东西,因此,如果需要编辑此代码,请告诉我。但是你能告诉我我在这里想念的是什么吗?
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。